protobufjs 8.6.1 → 8.6.2
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/LICENSE +39 -39
- package/README.md +441 -441
- package/dist/light/protobuf.js +8455 -8455
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +2999 -2999
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +10359 -10359
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +70 -70
- package/ext/debug/README.md +4 -4
- package/ext/debug/index.js +71 -71
- package/ext/descriptor/README.md +5 -5
- package/ext/descriptor/index.d.ts +2 -2
- package/ext/descriptor/index.js +2 -2
- package/ext/descriptor.d.ts +87 -87
- package/ext/descriptor.js +1354 -1354
- package/ext/protojson.LICENSE +201 -201
- package/ext/protojson.d.ts +20 -20
- package/ext/protojson.js +925 -925
- package/ext/textformat.d.ts +19 -19
- package/ext/textformat.js +1256 -1256
- package/google/LICENSE +27 -27
- package/google/README.md +1 -1
- package/google/api/annotations.json +82 -82
- package/google/api/annotations.proto +10 -10
- package/google/api/http.json +85 -85
- package/google/api/http.proto +30 -30
- package/google/protobuf/api.json +117 -117
- package/google/protobuf/api.proto +33 -33
- package/google/protobuf/compiler/plugin.json +126 -126
- package/google/protobuf/compiler/plugin.proto +47 -47
- package/google/protobuf/descriptor.json +1381 -1381
- package/google/protobuf/descriptor.proto +534 -534
- package/google/protobuf/source_context.json +19 -19
- package/google/protobuf/source_context.proto +7 -7
- package/google/protobuf/type.json +201 -201
- package/google/protobuf/type.proto +89 -89
- package/index.d.ts +1 -1
- package/index.js +4 -4
- package/light.d.ts +2 -2
- package/light.js +3 -3
- package/minimal.d.ts +2 -2
- package/minimal.js +4 -4
- package/package.json +93 -93
- package/src/common.js +399 -399
- package/src/converter.js +344 -344
- package/src/decoder.js +208 -208
- package/src/encoder.js +111 -111
- package/src/enum.js +231 -231
- package/src/field.js +497 -497
- package/src/index-light.js +104 -104
- package/src/index-minimal.js +36 -36
- package/src/index.js +12 -12
- package/src/mapfield.js +136 -136
- package/src/message.js +137 -137
- package/src/method.js +175 -175
- package/src/namespace.js +565 -565
- package/src/object.js +382 -382
- package/src/oneof.js +225 -225
- package/src/parse.js +1068 -1068
- package/src/reader.js +543 -543
- package/src/reader_buffer.js +72 -72
- package/src/root.js +416 -416
- package/src/roots.js +18 -18
- package/src/rpc/service.js +148 -148
- package/src/rpc.js +36 -36
- package/src/service.js +198 -198
- package/src/tokenize.js +421 -421
- package/src/type.js +655 -655
- package/src/types.js +196 -196
- package/src/typescript.js +25 -25
- package/src/util/aspromise.d.ts +13 -13
- package/src/util/aspromise.js +52 -52
- package/src/util/base64.d.ts +32 -32
- package/src/util/base64.js +146 -146
- package/src/util/codegen.d.ts +31 -31
- package/src/util/codegen.js +113 -113
- package/src/util/eventemitter.d.ts +45 -45
- package/src/util/eventemitter.js +86 -86
- package/src/util/fetch.d.ts +56 -56
- package/src/util/fetch.js +112 -112
- package/src/util/float.d.ts +83 -83
- package/src/util/float.js +335 -335
- package/src/util/fs.js +11 -11
- package/src/util/longbits.js +200 -200
- package/src/util/minimal.js +515 -515
- package/src/util/path.d.ts +22 -22
- package/src/util/path.js +72 -72
- package/src/util/patterns.js +7 -7
- package/src/util/pool.d.ts +32 -32
- package/src/util/pool.js +48 -48
- package/src/util/utf8.d.ts +24 -24
- package/src/util/utf8.js +130 -130
- package/src/util.js +242 -242
- package/src/verifier.js +180 -180
- package/src/wrappers.js +106 -106
- package/src/writer.js +495 -495
- package/src/writer_buffer.js +102 -102
- package/tsconfig.json +6 -6
package/src/util/eventemitter.js
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
module.exports = EventEmitter;
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Constructs a new event emitter instance.
|
|
6
|
-
* @classdesc A minimal event emitter.
|
|
7
|
-
* @memberof util
|
|
8
|
-
* @constructor
|
|
9
|
-
*/
|
|
10
|
-
function EventEmitter() {
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Registered listeners.
|
|
14
|
-
* @type {Object.<string,*>}
|
|
15
|
-
* @private
|
|
16
|
-
*/
|
|
17
|
-
this._listeners = Object.create(null);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Event listener as used by {@link util.EventEmitter}.
|
|
22
|
-
* @typedef EventEmitterListener
|
|
23
|
-
* @type {function}
|
|
24
|
-
* @param {...*} args Arguments
|
|
25
|
-
* @returns {undefined}
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Registers an event listener.
|
|
30
|
-
* @param {string} evt Event name
|
|
31
|
-
* @param {EventEmitterListener} fn Listener
|
|
32
|
-
* @param {*} [ctx] Listener context
|
|
33
|
-
* @returns {this} `this`
|
|
34
|
-
*/
|
|
35
|
-
EventEmitter.prototype.on = function on(evt, fn, ctx) {
|
|
36
|
-
(this._listeners[evt] || (this._listeners[evt] = [])).push({
|
|
37
|
-
fn : fn,
|
|
38
|
-
ctx : ctx || this
|
|
39
|
-
});
|
|
40
|
-
return this;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Removes an event listener or any matching listeners if arguments are omitted.
|
|
45
|
-
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
|
46
|
-
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
|
47
|
-
* @returns {this} `this`
|
|
48
|
-
*/
|
|
49
|
-
EventEmitter.prototype.off = function off(evt, fn) {
|
|
50
|
-
if (evt === undefined)
|
|
51
|
-
this._listeners = Object.create(null);
|
|
52
|
-
else {
|
|
53
|
-
if (fn === undefined)
|
|
54
|
-
this._listeners[evt] = [];
|
|
55
|
-
else {
|
|
56
|
-
var listeners = this._listeners[evt];
|
|
57
|
-
if (!listeners)
|
|
58
|
-
return this;
|
|
59
|
-
for (var i = 0; i < listeners.length;)
|
|
60
|
-
if (listeners[i].fn === fn)
|
|
61
|
-
listeners.splice(i, 1);
|
|
62
|
-
else
|
|
63
|
-
++i;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return this;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Emits an event by calling its listeners with the specified arguments.
|
|
71
|
-
* @param {string} evt Event name
|
|
72
|
-
* @param {...*} args Arguments
|
|
73
|
-
* @returns {this} `this`
|
|
74
|
-
*/
|
|
75
|
-
EventEmitter.prototype.emit = function emit(evt) {
|
|
76
|
-
var listeners = this._listeners[evt];
|
|
77
|
-
if (listeners) {
|
|
78
|
-
var args = [],
|
|
79
|
-
i = 1;
|
|
80
|
-
for (; i < arguments.length;)
|
|
81
|
-
args.push(arguments[i++]);
|
|
82
|
-
for (i = 0; i < listeners.length;)
|
|
83
|
-
listeners[i].fn.apply(listeners[i++].ctx, args);
|
|
84
|
-
}
|
|
85
|
-
return this;
|
|
86
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = EventEmitter;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Constructs a new event emitter instance.
|
|
6
|
+
* @classdesc A minimal event emitter.
|
|
7
|
+
* @memberof util
|
|
8
|
+
* @constructor
|
|
9
|
+
*/
|
|
10
|
+
function EventEmitter() {
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Registered listeners.
|
|
14
|
+
* @type {Object.<string,*>}
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
this._listeners = Object.create(null);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Event listener as used by {@link util.EventEmitter}.
|
|
22
|
+
* @typedef EventEmitterListener
|
|
23
|
+
* @type {function}
|
|
24
|
+
* @param {...*} args Arguments
|
|
25
|
+
* @returns {undefined}
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Registers an event listener.
|
|
30
|
+
* @param {string} evt Event name
|
|
31
|
+
* @param {EventEmitterListener} fn Listener
|
|
32
|
+
* @param {*} [ctx] Listener context
|
|
33
|
+
* @returns {this} `this`
|
|
34
|
+
*/
|
|
35
|
+
EventEmitter.prototype.on = function on(evt, fn, ctx) {
|
|
36
|
+
(this._listeners[evt] || (this._listeners[evt] = [])).push({
|
|
37
|
+
fn : fn,
|
|
38
|
+
ctx : ctx || this
|
|
39
|
+
});
|
|
40
|
+
return this;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Removes an event listener or any matching listeners if arguments are omitted.
|
|
45
|
+
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
|
46
|
+
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
|
47
|
+
* @returns {this} `this`
|
|
48
|
+
*/
|
|
49
|
+
EventEmitter.prototype.off = function off(evt, fn) {
|
|
50
|
+
if (evt === undefined)
|
|
51
|
+
this._listeners = Object.create(null);
|
|
52
|
+
else {
|
|
53
|
+
if (fn === undefined)
|
|
54
|
+
this._listeners[evt] = [];
|
|
55
|
+
else {
|
|
56
|
+
var listeners = this._listeners[evt];
|
|
57
|
+
if (!listeners)
|
|
58
|
+
return this;
|
|
59
|
+
for (var i = 0; i < listeners.length;)
|
|
60
|
+
if (listeners[i].fn === fn)
|
|
61
|
+
listeners.splice(i, 1);
|
|
62
|
+
else
|
|
63
|
+
++i;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return this;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Emits an event by calling its listeners with the specified arguments.
|
|
71
|
+
* @param {string} evt Event name
|
|
72
|
+
* @param {...*} args Arguments
|
|
73
|
+
* @returns {this} `this`
|
|
74
|
+
*/
|
|
75
|
+
EventEmitter.prototype.emit = function emit(evt) {
|
|
76
|
+
var listeners = this._listeners[evt];
|
|
77
|
+
if (listeners) {
|
|
78
|
+
var args = [],
|
|
79
|
+
i = 1;
|
|
80
|
+
for (; i < arguments.length;)
|
|
81
|
+
args.push(arguments[i++]);
|
|
82
|
+
for (i = 0; i < listeners.length;)
|
|
83
|
+
listeners[i].fn.apply(listeners[i++].ctx, args);
|
|
84
|
+
}
|
|
85
|
+
return this;
|
|
86
|
+
};
|
package/src/util/fetch.d.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
export = fetch;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Node-style callback as used by {@link util.fetch}.
|
|
5
|
-
* @typedef FetchCallback
|
|
6
|
-
* @type {function}
|
|
7
|
-
* @param {?Error} error Error, if any, otherwise `null`
|
|
8
|
-
* @param {string} [contents] File contents, if there hasn't been an error
|
|
9
|
-
* @returns {undefined}
|
|
10
|
-
*/
|
|
11
|
-
type FetchCallback = (error: Error, contents?: string) => void;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Options as used by {@link util.fetch}.
|
|
15
|
-
* @typedef IFetchOptions
|
|
16
|
-
* @type {Object}
|
|
17
|
-
* @property {boolean} [binary=false] Whether expecting a binary response
|
|
18
|
-
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
interface IFetchOptions {
|
|
22
|
-
binary?: boolean;
|
|
23
|
-
xhr?: boolean;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Fetches the contents of a file.
|
|
28
|
-
* @memberof util
|
|
29
|
-
* @param {string} filename File path or url
|
|
30
|
-
* @param {IFetchOptions} options Fetch options
|
|
31
|
-
* @param {FetchCallback} callback Callback function
|
|
32
|
-
* @returns {undefined}
|
|
33
|
-
*/
|
|
34
|
-
declare function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Fetches the contents of a file.
|
|
38
|
-
* @name util.fetch
|
|
39
|
-
* @function
|
|
40
|
-
* @param {string} path File path or url
|
|
41
|
-
* @param {FetchCallback} callback Callback function
|
|
42
|
-
* @returns {undefined}
|
|
43
|
-
* @variation 2
|
|
44
|
-
*/
|
|
45
|
-
declare function fetch(path: string, callback: FetchCallback): void;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Fetches the contents of a file.
|
|
49
|
-
* @name util.fetch
|
|
50
|
-
* @function
|
|
51
|
-
* @param {string} path File path or url
|
|
52
|
-
* @param {IFetchOptions} [options] Fetch options
|
|
53
|
-
* @returns {Promise<string|Uint8Array>} Promise
|
|
54
|
-
* @variation 3
|
|
55
|
-
*/
|
|
56
|
-
declare function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
|
|
1
|
+
export = fetch;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Node-style callback as used by {@link util.fetch}.
|
|
5
|
+
* @typedef FetchCallback
|
|
6
|
+
* @type {function}
|
|
7
|
+
* @param {?Error} error Error, if any, otherwise `null`
|
|
8
|
+
* @param {string} [contents] File contents, if there hasn't been an error
|
|
9
|
+
* @returns {undefined}
|
|
10
|
+
*/
|
|
11
|
+
type FetchCallback = (error: Error, contents?: string) => void;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Options as used by {@link util.fetch}.
|
|
15
|
+
* @typedef IFetchOptions
|
|
16
|
+
* @type {Object}
|
|
17
|
+
* @property {boolean} [binary=false] Whether expecting a binary response
|
|
18
|
+
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
interface IFetchOptions {
|
|
22
|
+
binary?: boolean;
|
|
23
|
+
xhr?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Fetches the contents of a file.
|
|
28
|
+
* @memberof util
|
|
29
|
+
* @param {string} filename File path or url
|
|
30
|
+
* @param {IFetchOptions} options Fetch options
|
|
31
|
+
* @param {FetchCallback} callback Callback function
|
|
32
|
+
* @returns {undefined}
|
|
33
|
+
*/
|
|
34
|
+
declare function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fetches the contents of a file.
|
|
38
|
+
* @name util.fetch
|
|
39
|
+
* @function
|
|
40
|
+
* @param {string} path File path or url
|
|
41
|
+
* @param {FetchCallback} callback Callback function
|
|
42
|
+
* @returns {undefined}
|
|
43
|
+
* @variation 2
|
|
44
|
+
*/
|
|
45
|
+
declare function fetch(path: string, callback: FetchCallback): void;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Fetches the contents of a file.
|
|
49
|
+
* @name util.fetch
|
|
50
|
+
* @function
|
|
51
|
+
* @param {string} path File path or url
|
|
52
|
+
* @param {IFetchOptions} [options] Fetch options
|
|
53
|
+
* @returns {Promise<string|Uint8Array>} Promise
|
|
54
|
+
* @variation 3
|
|
55
|
+
*/
|
|
56
|
+
declare function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
|
package/src/util/fetch.js
CHANGED
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
module.exports = fetch;
|
|
3
|
-
|
|
4
|
-
var asPromise = require("./aspromise"),
|
|
5
|
-
fs = require("./fs");
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Node-style callback as used by {@link util.fetch}.
|
|
9
|
-
* @typedef FetchCallback
|
|
10
|
-
* @type {function}
|
|
11
|
-
* @param {?Error} error Error, if any, otherwise `null`
|
|
12
|
-
* @param {string} [contents] File contents, if there hasn't been an error
|
|
13
|
-
* @returns {undefined}
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Options as used by {@link util.fetch}.
|
|
18
|
-
* @interface IFetchOptions
|
|
19
|
-
* @property {boolean} [binary=false] Whether expecting a binary response
|
|
20
|
-
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Fetches the contents of a file.
|
|
25
|
-
* @memberof util
|
|
26
|
-
* @param {string} filename File path or url
|
|
27
|
-
* @param {IFetchOptions} options Fetch options
|
|
28
|
-
* @param {FetchCallback} callback Callback function
|
|
29
|
-
* @returns {undefined}
|
|
30
|
-
*/
|
|
31
|
-
function fetch(filename, options, callback) {
|
|
32
|
-
if (typeof options === "function") {
|
|
33
|
-
callback = options;
|
|
34
|
-
options = {};
|
|
35
|
-
} else if (!options)
|
|
36
|
-
options = {};
|
|
37
|
-
|
|
38
|
-
if (!callback)
|
|
39
|
-
return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this
|
|
40
|
-
|
|
41
|
-
// if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.
|
|
42
|
-
if (!options.xhr && fs && fs.readFile)
|
|
43
|
-
return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
|
|
44
|
-
return err && typeof XMLHttpRequest !== "undefined"
|
|
45
|
-
? fetch.xhr(filename, options, callback)
|
|
46
|
-
: err
|
|
47
|
-
? callback(err)
|
|
48
|
-
: callback(null, options.binary ? contents : contents.toString("utf8"));
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// use the XHR version otherwise.
|
|
52
|
-
return fetch.xhr(filename, options, callback);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Fetches the contents of a file.
|
|
57
|
-
* @name util.fetch
|
|
58
|
-
* @function
|
|
59
|
-
* @param {string} path File path or url
|
|
60
|
-
* @param {FetchCallback} callback Callback function
|
|
61
|
-
* @returns {undefined}
|
|
62
|
-
* @variation 2
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Fetches the contents of a file.
|
|
67
|
-
* @name util.fetch
|
|
68
|
-
* @function
|
|
69
|
-
* @param {string} path File path or url
|
|
70
|
-
* @param {IFetchOptions} [options] Fetch options
|
|
71
|
-
* @returns {Promise<string|Uint8Array>} Promise
|
|
72
|
-
* @variation 3
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
/**/
|
|
76
|
-
fetch.xhr = function fetch_xhr(filename, options, callback) {
|
|
77
|
-
var xhr = new XMLHttpRequest();
|
|
78
|
-
xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {
|
|
79
|
-
|
|
80
|
-
if (xhr.readyState !== 4)
|
|
81
|
-
return undefined;
|
|
82
|
-
|
|
83
|
-
// local cors security errors return status 0 / empty string, too. afaik this cannot be
|
|
84
|
-
// reliably distinguished from an actually empty file for security reasons. feel free
|
|
85
|
-
// to send a pull request if you are aware of a solution.
|
|
86
|
-
if (xhr.status !== 0 && xhr.status !== 200)
|
|
87
|
-
return callback(Error("status " + xhr.status));
|
|
88
|
-
|
|
89
|
-
// if binary data is expected, make sure that some sort of array is returned, even if
|
|
90
|
-
// ArrayBuffers are not supported. the binary string fallback, however, is unsafe.
|
|
91
|
-
if (options.binary) {
|
|
92
|
-
var buffer = xhr.response;
|
|
93
|
-
if (!buffer) {
|
|
94
|
-
buffer = [];
|
|
95
|
-
for (var i = 0; i < xhr.responseText.length; ++i)
|
|
96
|
-
buffer.push(xhr.responseText.charCodeAt(i) & 255);
|
|
97
|
-
}
|
|
98
|
-
return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
|
|
99
|
-
}
|
|
100
|
-
return callback(null, xhr.responseText);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
if (options.binary) {
|
|
104
|
-
// ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers
|
|
105
|
-
if ("overrideMimeType" in xhr)
|
|
106
|
-
xhr.overrideMimeType("text/plain; charset=x-user-defined");
|
|
107
|
-
xhr.responseType = "arraybuffer";
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
xhr.open("GET", filename);
|
|
111
|
-
xhr.send();
|
|
112
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = fetch;
|
|
3
|
+
|
|
4
|
+
var asPromise = require("./aspromise"),
|
|
5
|
+
fs = require("./fs");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Node-style callback as used by {@link util.fetch}.
|
|
9
|
+
* @typedef FetchCallback
|
|
10
|
+
* @type {function}
|
|
11
|
+
* @param {?Error} error Error, if any, otherwise `null`
|
|
12
|
+
* @param {string} [contents] File contents, if there hasn't been an error
|
|
13
|
+
* @returns {undefined}
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Options as used by {@link util.fetch}.
|
|
18
|
+
* @interface IFetchOptions
|
|
19
|
+
* @property {boolean} [binary=false] Whether expecting a binary response
|
|
20
|
+
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Fetches the contents of a file.
|
|
25
|
+
* @memberof util
|
|
26
|
+
* @param {string} filename File path or url
|
|
27
|
+
* @param {IFetchOptions} options Fetch options
|
|
28
|
+
* @param {FetchCallback} callback Callback function
|
|
29
|
+
* @returns {undefined}
|
|
30
|
+
*/
|
|
31
|
+
function fetch(filename, options, callback) {
|
|
32
|
+
if (typeof options === "function") {
|
|
33
|
+
callback = options;
|
|
34
|
+
options = {};
|
|
35
|
+
} else if (!options)
|
|
36
|
+
options = {};
|
|
37
|
+
|
|
38
|
+
if (!callback)
|
|
39
|
+
return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this
|
|
40
|
+
|
|
41
|
+
// if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.
|
|
42
|
+
if (!options.xhr && fs && fs.readFile)
|
|
43
|
+
return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
|
|
44
|
+
return err && typeof XMLHttpRequest !== "undefined"
|
|
45
|
+
? fetch.xhr(filename, options, callback)
|
|
46
|
+
: err
|
|
47
|
+
? callback(err)
|
|
48
|
+
: callback(null, options.binary ? contents : contents.toString("utf8"));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// use the XHR version otherwise.
|
|
52
|
+
return fetch.xhr(filename, options, callback);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Fetches the contents of a file.
|
|
57
|
+
* @name util.fetch
|
|
58
|
+
* @function
|
|
59
|
+
* @param {string} path File path or url
|
|
60
|
+
* @param {FetchCallback} callback Callback function
|
|
61
|
+
* @returns {undefined}
|
|
62
|
+
* @variation 2
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Fetches the contents of a file.
|
|
67
|
+
* @name util.fetch
|
|
68
|
+
* @function
|
|
69
|
+
* @param {string} path File path or url
|
|
70
|
+
* @param {IFetchOptions} [options] Fetch options
|
|
71
|
+
* @returns {Promise<string|Uint8Array>} Promise
|
|
72
|
+
* @variation 3
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**/
|
|
76
|
+
fetch.xhr = function fetch_xhr(filename, options, callback) {
|
|
77
|
+
var xhr = new XMLHttpRequest();
|
|
78
|
+
xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {
|
|
79
|
+
|
|
80
|
+
if (xhr.readyState !== 4)
|
|
81
|
+
return undefined;
|
|
82
|
+
|
|
83
|
+
// local cors security errors return status 0 / empty string, too. afaik this cannot be
|
|
84
|
+
// reliably distinguished from an actually empty file for security reasons. feel free
|
|
85
|
+
// to send a pull request if you are aware of a solution.
|
|
86
|
+
if (xhr.status !== 0 && xhr.status !== 200)
|
|
87
|
+
return callback(Error("status " + xhr.status));
|
|
88
|
+
|
|
89
|
+
// if binary data is expected, make sure that some sort of array is returned, even if
|
|
90
|
+
// ArrayBuffers are not supported. the binary string fallback, however, is unsafe.
|
|
91
|
+
if (options.binary) {
|
|
92
|
+
var buffer = xhr.response;
|
|
93
|
+
if (!buffer) {
|
|
94
|
+
buffer = [];
|
|
95
|
+
for (var i = 0; i < xhr.responseText.length; ++i)
|
|
96
|
+
buffer.push(xhr.responseText.charCodeAt(i) & 255);
|
|
97
|
+
}
|
|
98
|
+
return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
|
|
99
|
+
}
|
|
100
|
+
return callback(null, xhr.responseText);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
if (options.binary) {
|
|
104
|
+
// ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers
|
|
105
|
+
if ("overrideMimeType" in xhr)
|
|
106
|
+
xhr.overrideMimeType("text/plain; charset=x-user-defined");
|
|
107
|
+
xhr.responseType = "arraybuffer";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
xhr.open("GET", filename);
|
|
111
|
+
xhr.send();
|
|
112
|
+
};
|
package/src/util/float.d.ts
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Writes a 32 bit float to a buffer using little endian byte order.
|
|
3
|
-
* @name writeFloatLE
|
|
4
|
-
* @function
|
|
5
|
-
* @param {number} val Value to write
|
|
6
|
-
* @param {Uint8Array} buf Target buffer
|
|
7
|
-
* @param {number} pos Target buffer offset
|
|
8
|
-
* @returns {undefined}
|
|
9
|
-
*/
|
|
10
|
-
export function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Writes a 32 bit float to a buffer using big endian byte order.
|
|
14
|
-
* @name writeFloatBE
|
|
15
|
-
* @function
|
|
16
|
-
* @param {number} val Value to write
|
|
17
|
-
* @param {Uint8Array} buf Target buffer
|
|
18
|
-
* @param {number} pos Target buffer offset
|
|
19
|
-
* @returns {undefined}
|
|
20
|
-
*/
|
|
21
|
-
export function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Reads a 32 bit float from a buffer using little endian byte order.
|
|
25
|
-
* @name readFloatLE
|
|
26
|
-
* @function
|
|
27
|
-
* @param {Uint8Array} buf Source buffer
|
|
28
|
-
* @param {number} pos Source buffer offset
|
|
29
|
-
* @returns {number} Value read
|
|
30
|
-
*/
|
|
31
|
-
export function readFloatLE(buf: Uint8Array, pos: number): number;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Reads a 32 bit float from a buffer using big endian byte order.
|
|
35
|
-
* @name readFloatBE
|
|
36
|
-
* @function
|
|
37
|
-
* @param {Uint8Array} buf Source buffer
|
|
38
|
-
* @param {number} pos Source buffer offset
|
|
39
|
-
* @returns {number} Value read
|
|
40
|
-
*/
|
|
41
|
-
export function readFloatBE(buf: Uint8Array, pos: number): number;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Writes a 64 bit double to a buffer using little endian byte order.
|
|
45
|
-
* @name writeDoubleLE
|
|
46
|
-
* @function
|
|
47
|
-
* @param {number} val Value to write
|
|
48
|
-
* @param {Uint8Array} buf Target buffer
|
|
49
|
-
* @param {number} pos Target buffer offset
|
|
50
|
-
* @returns {undefined}
|
|
51
|
-
*/
|
|
52
|
-
export function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Writes a 64 bit double to a buffer using big endian byte order.
|
|
56
|
-
* @name writeDoubleBE
|
|
57
|
-
* @function
|
|
58
|
-
* @param {number} val Value to write
|
|
59
|
-
* @param {Uint8Array} buf Target buffer
|
|
60
|
-
* @param {number} pos Target buffer offset
|
|
61
|
-
* @returns {undefined}
|
|
62
|
-
*/
|
|
63
|
-
export function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Reads a 64 bit double from a buffer using little endian byte order.
|
|
67
|
-
* @name readDoubleLE
|
|
68
|
-
* @function
|
|
69
|
-
* @param {Uint8Array} buf Source buffer
|
|
70
|
-
* @param {number} pos Source buffer offset
|
|
71
|
-
* @returns {number} Value read
|
|
72
|
-
*/
|
|
73
|
-
export function readDoubleLE(buf: Uint8Array, pos: number): number;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Reads a 64 bit double from a buffer using big endian byte order.
|
|
77
|
-
* @name readDoubleBE
|
|
78
|
-
* @function
|
|
79
|
-
* @param {Uint8Array} buf Source buffer
|
|
80
|
-
* @param {number} pos Source buffer offset
|
|
81
|
-
* @returns {number} Value read
|
|
82
|
-
*/
|
|
83
|
-
export function readDoubleBE(buf: Uint8Array, pos: number): number;
|
|
1
|
+
/**
|
|
2
|
+
* Writes a 32 bit float to a buffer using little endian byte order.
|
|
3
|
+
* @name writeFloatLE
|
|
4
|
+
* @function
|
|
5
|
+
* @param {number} val Value to write
|
|
6
|
+
* @param {Uint8Array} buf Target buffer
|
|
7
|
+
* @param {number} pos Target buffer offset
|
|
8
|
+
* @returns {undefined}
|
|
9
|
+
*/
|
|
10
|
+
export function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Writes a 32 bit float to a buffer using big endian byte order.
|
|
14
|
+
* @name writeFloatBE
|
|
15
|
+
* @function
|
|
16
|
+
* @param {number} val Value to write
|
|
17
|
+
* @param {Uint8Array} buf Target buffer
|
|
18
|
+
* @param {number} pos Target buffer offset
|
|
19
|
+
* @returns {undefined}
|
|
20
|
+
*/
|
|
21
|
+
export function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reads a 32 bit float from a buffer using little endian byte order.
|
|
25
|
+
* @name readFloatLE
|
|
26
|
+
* @function
|
|
27
|
+
* @param {Uint8Array} buf Source buffer
|
|
28
|
+
* @param {number} pos Source buffer offset
|
|
29
|
+
* @returns {number} Value read
|
|
30
|
+
*/
|
|
31
|
+
export function readFloatLE(buf: Uint8Array, pos: number): number;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Reads a 32 bit float from a buffer using big endian byte order.
|
|
35
|
+
* @name readFloatBE
|
|
36
|
+
* @function
|
|
37
|
+
* @param {Uint8Array} buf Source buffer
|
|
38
|
+
* @param {number} pos Source buffer offset
|
|
39
|
+
* @returns {number} Value read
|
|
40
|
+
*/
|
|
41
|
+
export function readFloatBE(buf: Uint8Array, pos: number): number;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Writes a 64 bit double to a buffer using little endian byte order.
|
|
45
|
+
* @name writeDoubleLE
|
|
46
|
+
* @function
|
|
47
|
+
* @param {number} val Value to write
|
|
48
|
+
* @param {Uint8Array} buf Target buffer
|
|
49
|
+
* @param {number} pos Target buffer offset
|
|
50
|
+
* @returns {undefined}
|
|
51
|
+
*/
|
|
52
|
+
export function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Writes a 64 bit double to a buffer using big endian byte order.
|
|
56
|
+
* @name writeDoubleBE
|
|
57
|
+
* @function
|
|
58
|
+
* @param {number} val Value to write
|
|
59
|
+
* @param {Uint8Array} buf Target buffer
|
|
60
|
+
* @param {number} pos Target buffer offset
|
|
61
|
+
* @returns {undefined}
|
|
62
|
+
*/
|
|
63
|
+
export function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Reads a 64 bit double from a buffer using little endian byte order.
|
|
67
|
+
* @name readDoubleLE
|
|
68
|
+
* @function
|
|
69
|
+
* @param {Uint8Array} buf Source buffer
|
|
70
|
+
* @param {number} pos Source buffer offset
|
|
71
|
+
* @returns {number} Value read
|
|
72
|
+
*/
|
|
73
|
+
export function readDoubleLE(buf: Uint8Array, pos: number): number;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Reads a 64 bit double from a buffer using big endian byte order.
|
|
77
|
+
* @name readDoubleBE
|
|
78
|
+
* @function
|
|
79
|
+
* @param {Uint8Array} buf Source buffer
|
|
80
|
+
* @param {number} pos Source buffer offset
|
|
81
|
+
* @returns {number} Value read
|
|
82
|
+
*/
|
|
83
|
+
export function readDoubleBE(buf: Uint8Array, pos: number): number;
|