@loaders.gl/polyfills 3.4.0-alpha.1 → 3.4.0-alpha.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/dist/dist.min.js CHANGED
@@ -4111,6 +4111,7 @@
4111
4111
  }
4112
4112
  if (!isBrowser && !("_parseImageNode" in global_) && import_parse_image.parseImageNode) {
4113
4113
  global_["_parseImageNode"] = import_parse_image.parseImageNode;
4114
+ global_["_imageFormatsNode"] = import_parse_image.NODE_FORMAT_SUPPORT;
4114
4115
  }
4115
4116
  if (!("allSettled" in Promise)) {
4116
4117
  Promise.allSettled = allSettled;
package/dist/es5/index.js CHANGED
@@ -100,6 +100,7 @@ if (!_globals.isBrowser && !('_encodeImageNode' in _globals.global) && _encodeIm
100
100
  }
101
101
  if (!_globals.isBrowser && !('_parseImageNode' in _globals.global) && _parseImage.parseImageNode) {
102
102
  _globals.global['_parseImageNode'] = _parseImage.parseImageNode;
103
+ _globals.global['_imageFormatsNode'] = _parseImage.NODE_FORMAT_SUPPORT;
103
104
  }
104
105
  if (!('allSettled' in Promise)) {
105
106
  Promise.allSettled = _allSettled.allSettled;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["installTextEncoder","isBrowser","global","TextEncoder","installTextDecoder","TextDecoder","base64","atob","btoa","HeadersNode","ResponseNode","fetchNode","DOMParser","encodeImageNode","parseImageNode","Promise","allSettled"],"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable dot-notation */\nimport {DOMParser} from '@xmldom/xmldom';\nimport {isBrowser, global} from './utils/globals';\n\nimport {TextDecoder, TextEncoder} from './lib/encoding';\nimport {allSettled} from './promise/all-settled';\n\n// Node specific\nimport * as base64 from './node/buffer/btoa.node';\n\nimport {Headers as HeadersNode} from './node/fetch/headers.node';\nimport {Response as ResponseNode} from './node/fetch/response.node';\nimport {fetchNode as fetchNode} from './node/fetch/fetch.node';\n\nimport {encodeImageNode} from './node/images/encode-image.node';\nimport {parseImageNode} from './node/images/parse-image.node';\n\nexport {ReadableStreamPolyfill} from './node/file/readable-stream';\nexport {BlobPolyfill} from './node/file/blob';\nexport {FileReaderPolyfill} from './node/file/file-reader';\nexport {FilePolyfill} from './node/file/file';\nexport {installFilePolyfills} from './node/file/install-file-polyfills';\nexport {fetchNode as _fetchNode} from './node/fetch/fetch.node';\nexport {fetchFileNode as _fetchFileNode} from './node/fetch/fetch-file.node';\n\n// POLYFILLS: TextEncoder, TextDecoder\n// - Recent Node versions have these classes but virtually no encodings unless special build.\n// - Browser: Edge, IE11 do not have these\n\nconst installTextEncoder = !isBrowser || !('TextEncoder' in global);\nif (installTextEncoder) {\n global['TextEncoder'] = TextEncoder;\n}\n\nconst installTextDecoder = !isBrowser || !('TextDecoder' in global);\nif (installTextDecoder) {\n global['TextDecoder'] = TextDecoder;\n}\n\n// POLYFILLS: btoa, atob\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('atob' in global) && base64.atob) {\n global['atob'] = base64.atob;\n}\nif (!isBrowser && !('btoa' in global) && base64.btoa) {\n global['btoa'] = base64.btoa;\n}\n\n// POLYFILL: fetch\n// - Node: Yes\n// - Browser: No. For This polyfill is node only, IE11 etc, install external polyfill\n\nif (!isBrowser && !('Headers' in global) && HeadersNode) {\n global['Headers'] = HeadersNode;\n}\n\nif (!isBrowser && !('Response' in global) && ResponseNode) {\n global['Response'] = ResponseNode;\n}\n\nif (!isBrowser && !('fetch' in global) && fetchNode) {\n global['fetch'] = fetchNode;\n}\n\n// POLYFILL: DOMParser\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('DOMParser' in global) && DOMParser) {\n global['DOMParser'] = DOMParser;\n}\n\n// NODE IMAGE FUNCTIONS:\n// These are not official polyfills but used by the @loaders.gl/images module if installed\n// TODO - is there an appropriate Image API we could polyfill using an adapter?\n\nif (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {\n global['_encodeImageNode'] = encodeImageNode;\n}\n\nif (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {\n global['_parseImageNode'] = parseImageNode;\n}\n\nif (!('allSettled' in Promise)) {\n // @ts-ignore\n Promise.allSettled = allSettled;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA;AACA;AAEA;AACA;AAGA;AAEA;AACA;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA;AACA;AAEA;AAA6E;AAAA;;AAM7E,IAAMA,kBAAkB,GAAG,CAACC,kBAAS,IAAI,EAAE,aAAa,IAAIC,eAAM,CAAC;AACnE,IAAIF,kBAAkB,EAAE;EACtBE,eAAM,CAAC,aAAa,CAAC,GAAGC,qBAAW;AACrC;AAEA,IAAMC,kBAAkB,GAAG,CAACH,kBAAS,IAAI,EAAE,aAAa,IAAIC,eAAM,CAAC;AACnE,IAAIE,kBAAkB,EAAE;EACtBF,eAAM,CAAC,aAAa,CAAC,GAAGG,qBAAW;AACrC;;AAMA,IAAI,CAACJ,kBAAS,IAAI,EAAE,MAAM,IAAIC,eAAM,CAAC,IAAII,MAAM,CAACC,IAAI,EAAE;EACpDL,eAAM,CAAC,MAAM,CAAC,GAAGI,MAAM,CAACC,IAAI;AAC9B;AACA,IAAI,CAACN,kBAAS,IAAI,EAAE,MAAM,IAAIC,eAAM,CAAC,IAAII,MAAM,CAACE,IAAI,EAAE;EACpDN,eAAM,CAAC,MAAM,CAAC,GAAGI,MAAM,CAACE,IAAI;AAC9B;;AAMA,IAAI,CAACP,kBAAS,IAAI,EAAE,SAAS,IAAIC,eAAM,CAAC,IAAIO,gBAAW,EAAE;EACvDP,eAAM,CAAC,SAAS,CAAC,GAAGO,gBAAW;AACjC;AAEA,IAAI,CAACR,kBAAS,IAAI,EAAE,UAAU,IAAIC,eAAM,CAAC,IAAIQ,kBAAY,EAAE;EACzDR,eAAM,CAAC,UAAU,CAAC,GAAGQ,kBAAY;AACnC;AAEA,IAAI,CAACT,kBAAS,IAAI,EAAE,OAAO,IAAIC,eAAM,CAAC,IAAIS,gBAAS,EAAE;EACnDT,eAAM,CAAC,OAAO,CAAC,GAAGS,gBAAS;AAC7B;;AAMA,IAAI,CAACV,kBAAS,IAAI,EAAE,WAAW,IAAIC,eAAM,CAAC,IAAIU,iBAAS,EAAE;EACvDV,eAAM,CAAC,WAAW,CAAC,GAAGU,iBAAS;AACjC;;AAMA,IAAI,CAACX,kBAAS,IAAI,EAAE,kBAAkB,IAAIC,eAAM,CAAC,IAAIW,4BAAe,EAAE;EACpEX,eAAM,CAAC,kBAAkB,CAAC,GAAGW,4BAAe;AAC9C;AAEA,IAAI,CAACZ,kBAAS,IAAI,EAAE,iBAAiB,IAAIC,eAAM,CAAC,IAAIY,0BAAc,EAAE;EAClEZ,eAAM,CAAC,iBAAiB,CAAC,GAAGY,0BAAc;AAC5C;AAEA,IAAI,EAAE,YAAY,IAAIC,OAAO,CAAC,EAAE;EAE9BA,OAAO,CAACC,UAAU,GAAGA,sBAAU;AACjC"}
1
+ {"version":3,"file":"index.js","names":["installTextEncoder","isBrowser","global","TextEncoder","installTextDecoder","TextDecoder","base64","atob","btoa","HeadersNode","ResponseNode","fetchNode","DOMParser","encodeImageNode","parseImageNode","NODE_FORMAT_SUPPORT","Promise","allSettled"],"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable dot-notation */\nimport {DOMParser} from '@xmldom/xmldom';\nimport {isBrowser, global} from './utils/globals';\n\nimport {TextDecoder, TextEncoder} from './lib/encoding';\nimport {allSettled} from './promise/all-settled';\n\n// Node specific\nimport * as base64 from './node/buffer/btoa.node';\n\nimport {Headers as HeadersNode} from './node/fetch/headers.node';\nimport {Response as ResponseNode} from './node/fetch/response.node';\nimport {fetchNode as fetchNode} from './node/fetch/fetch.node';\n\nimport {encodeImageNode} from './node/images/encode-image.node';\nimport {parseImageNode, NODE_FORMAT_SUPPORT} from './node/images/parse-image.node';\n\nexport {ReadableStreamPolyfill} from './node/file/readable-stream';\nexport {BlobPolyfill} from './node/file/blob';\nexport {FileReaderPolyfill} from './node/file/file-reader';\nexport {FilePolyfill} from './node/file/file';\nexport {installFilePolyfills} from './node/file/install-file-polyfills';\nexport {fetchNode as _fetchNode} from './node/fetch/fetch.node';\nexport {fetchFileNode as _fetchFileNode} from './node/fetch/fetch-file.node';\n\n// POLYFILLS: TextEncoder, TextDecoder\n// - Recent Node versions have these classes but virtually no encodings unless special build.\n// - Browser: Edge, IE11 do not have these\n\nconst installTextEncoder = !isBrowser || !('TextEncoder' in global);\nif (installTextEncoder) {\n global['TextEncoder'] = TextEncoder;\n}\n\nconst installTextDecoder = !isBrowser || !('TextDecoder' in global);\nif (installTextDecoder) {\n global['TextDecoder'] = TextDecoder;\n}\n\n// POLYFILLS: btoa, atob\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('atob' in global) && base64.atob) {\n global['atob'] = base64.atob;\n}\nif (!isBrowser && !('btoa' in global) && base64.btoa) {\n global['btoa'] = base64.btoa;\n}\n\n// POLYFILL: fetch\n// - Node: Yes\n// - Browser: No. For This polyfill is node only, IE11 etc, install external polyfill\n\nif (!isBrowser && !('Headers' in global) && HeadersNode) {\n global['Headers'] = HeadersNode;\n}\n\nif (!isBrowser && !('Response' in global) && ResponseNode) {\n global['Response'] = ResponseNode;\n}\n\nif (!isBrowser && !('fetch' in global) && fetchNode) {\n global['fetch'] = fetchNode;\n}\n\n// POLYFILL: DOMParser\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('DOMParser' in global) && DOMParser) {\n global['DOMParser'] = DOMParser;\n}\n\n// NODE IMAGE FUNCTIONS:\n// These are not official polyfills but used by the @loaders.gl/images module if installed\n// TODO - is there an appropriate Image API we could polyfill using an adapter?\n\nif (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {\n global['_encodeImageNode'] = encodeImageNode;\n}\n\nif (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {\n global['_parseImageNode'] = parseImageNode;\n global['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;\n}\n\nif (!('allSettled' in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S,gBAAS;AAC7B;;AAMA,IAAI,CAACV,kBAAS,IAAI,EAAE,WAAW,IAAIC,eAAM,CAAC,IAAIU,iBAAS,EAAE;EACvDV,eAAM,CAAC,WAAW,CAAC,GAAGU,iBAAS;AACjC;;AAMA,IAAI,CAACX,kBAAS,IAAI,EAAE,kBAAkB,IAAIC,eAAM,CAAC,IAAIW,4BAAe,EAAE;EACpEX,eAAM,CAAC,kBAAkB,CAAC,GAAGW,4BAAe;AAC9C;AAEA,IAAI,CAACZ,kBAAS,IAAI,EAAE,iBAAiB,IAAIC,eAAM,CAAC,IAAIY,0BAAc,EAAE;EAClEZ,eAAM,CAAC,iBAAiB,CAAC,GAAGY,0BAAc;EAC1CZ,eAAM,CAAC,mBAAmB,CAAC,GAAGa,+BAAmB;AACnD;AAEA,IAAI,EAAE,YAAY,IAAIC,OAAO,CAAC,EAAE;EAE9BA,OAAO,CAACC,UAAU,GAAGA,sBAAU;AACjC"}
@@ -4,43 +4,39 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
+ exports.NODE_FORMAT_SUPPORT = void 0;
7
8
  exports.parseImageNode = parseImageNode;
8
9
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
10
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
11
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
12
  var _getPixels = _interopRequireDefault(require("get-pixels"));
12
- var _assert = require("../../utils/assert");
13
- var _util = _interopRequireDefault(require("util"));
13
+
14
+ var NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
15
+
16
+ exports.NODE_FORMAT_SUPPORT = NODE_FORMAT_SUPPORT;
14
17
  function parseImageNode(_x, _x2) {
15
18
  return _parseImageNode.apply(this, arguments);
16
19
  }
17
20
  function _parseImageNode() {
18
21
  _parseImageNode = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(arrayBuffer, mimeType) {
19
- var getPixelsAsync, buffer, ndarray, shape, layers, data;
22
+ var buffer, ndarray;
20
23
  return _regenerator.default.wrap(function _callee$(_context) {
21
24
  while (1) {
22
25
  switch (_context.prev = _context.next) {
23
26
  case 0:
24
- (0, _assert.assert)(mimeType, 'MIMEType is required to parse image under Node.js');
25
-
26
- getPixelsAsync = _util.default.promisify(_getPixels.default);
27
+ if (mimeType) {
28
+ _context.next = 2;
29
+ break;
30
+ }
31
+ throw new Error('MIMEType is required to parse image under Node.js');
32
+ case 2:
27
33
  buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
28
34
  _context.next = 5;
29
35
  return getPixelsAsync(buffer, mimeType);
30
36
  case 5:
31
37
  ndarray = _context.sent;
32
- shape = (0, _toConsumableArray2.default)(ndarray.shape);
33
- layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
34
- data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
35
- return _context.abrupt("return", {
36
- shape: shape,
37
- data: data,
38
- width: ndarray.shape[0],
39
- height: ndarray.shape[1],
40
- components: ndarray.shape[2],
41
- layers: layers
42
- });
43
- case 10:
38
+ return _context.abrupt("return", ndarray);
39
+ case 7:
44
40
  case "end":
45
41
  return _context.stop();
46
42
  }
@@ -49,4 +45,25 @@ function _parseImageNode() {
49
45
  }));
50
46
  return _parseImageNode.apply(this, arguments);
51
47
  }
48
+ function getPixelsAsync(buffer, mimeType) {
49
+ return new Promise(function (resolve) {
50
+ return (0, _getPixels.default)(buffer, mimeType, function (err, ndarray) {
51
+ if (err) {
52
+ throw err;
53
+ }
54
+ var shape = (0, _toConsumableArray2.default)(ndarray.shape);
55
+ var layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
56
+ var data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
57
+
58
+ resolve({
59
+ shape: shape,
60
+ data: data,
61
+ width: ndarray.shape[0],
62
+ height: ndarray.shape[1],
63
+ components: ndarray.shape[2],
64
+ layers: layers ? [layers] : []
65
+ });
66
+ });
67
+ });
68
+ }
52
69
  //# sourceMappingURL=parse-image.node.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-image.node.js","names":["parseImageNode","arrayBuffer","mimeType","assert","getPixelsAsync","util","promisify","getPixels","buffer","Buffer","from","ndarray","shape","layers","length","shift","data","Uint8Array","width","height","components"],"sources":["../../../../src/node/images/parse-image.node.ts"],"sourcesContent":["import getPixels from 'get-pixels';\nimport {assert} from '../../utils/assert';\nimport util from 'util';\n\n// Note: These types are also defined in @loaders.gl/images and need to be kept in sync\ntype NDArray = {\n shape: number[];\n data: Uint8Array;\n width: number;\n height: number;\n components: number;\n layers: number[];\n};\n\nexport async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {\n assert(mimeType, 'MIMEType is required to parse image under Node.js');\n\n // TODO - check if getPixels callback is asynchronous if provided with buffer input\n // if not, parseImage can be a sync function\n const getPixelsAsync = util.promisify(getPixels);\n\n const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);\n\n const ndarray = await getPixelsAsync(buffer, mimeType);\n\n const shape = [...ndarray.shape];\n const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;\n const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;\n\n // extract width/height etc\n return {\n shape,\n data,\n width: ndarray.shape[0],\n height: ndarray.shape[1],\n components: ndarray.shape[2],\n layers\n };\n}\n"],"mappings":";;;;;;;;;;AAAA;AACA;AACA;AAAwB,SAYFA,cAAc;EAAA;AAAA;AAAA;EAAA,4EAA7B,iBAA8BC,WAAwB,EAAEC,QAAgB;IAAA;IAAA;MAAA;QAAA;UAAA;YAC7E,IAAAC,cAAM,EAACD,QAAQ,EAAE,mDAAmD,CAAC;;YAI/DE,cAAc,GAAGC,aAAI,CAACC,SAAS,CAACC,kBAAS,CAAC;YAE1CC,MAAM,GAAGP,WAAW,YAAYQ,MAAM,GAAGR,WAAW,GAAGQ,MAAM,CAACC,IAAI,CAACT,WAAW,CAAC;YAAA;YAAA,OAE/DG,cAAc,CAACI,MAAM,EAAEN,QAAQ,CAAC;UAAA;YAAhDS,OAAO;YAEPC,KAAK,oCAAOD,OAAO,CAACC,KAAK;YACzBC,MAAM,GAAGF,OAAO,CAACC,KAAK,CAACE,MAAM,KAAK,CAAC,GAAGH,OAAO,CAACC,KAAK,CAACG,KAAK,EAAE,GAAG,CAAC;YAC/DC,IAAI,GAAGL,OAAO,CAACK,IAAI,YAAYP,MAAM,GAAG,IAAIQ,UAAU,CAACN,OAAO,CAACK,IAAI,CAAC,GAAGL,OAAO,CAACK,IAAI;YAAA,iCAGlF;cACLJ,KAAK,EAALA,KAAK;cACLI,IAAI,EAAJA,IAAI;cACJE,KAAK,EAAEP,OAAO,CAACC,KAAK,CAAC,CAAC,CAAC;cACvBO,MAAM,EAAER,OAAO,CAACC,KAAK,CAAC,CAAC,CAAC;cACxBQ,UAAU,EAAET,OAAO,CAACC,KAAK,CAAC,CAAC,CAAC;cAC5BC,MAAM,EAANA;YACF,CAAC;UAAA;UAAA;YAAA;QAAA;MAAA;IAAA;EAAA,CACF;EAAA;AAAA"}
1
+ {"version":3,"file":"parse-image.node.js","names":["NODE_FORMAT_SUPPORT","parseImageNode","arrayBuffer","mimeType","Error","buffer","Buffer","from","getPixelsAsync","ndarray","Promise","resolve","getPixels","err","shape","layers","length","shift","data","Uint8Array","width","height","components"],"sources":["../../../../src/node/images/parse-image.node.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport getPixels from 'get-pixels';\n\n/** Declares which image format mime types this loader polyfill supports */\nexport const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];\n\n// Note: These types are also defined in @loaders.gl/images and need to be kept in sync\ntype NDArray = {\n shape: number[];\n data: Uint8Array;\n width: number;\n height: number;\n components: number;\n layers: number[];\n};\n\nexport async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {\n if (!mimeType) {\n throw new Error('MIMEType is required to parse image under Node.js');\n }\n\n const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);\n const ndarray = await getPixelsAsync(buffer, mimeType);\n return ndarray;\n}\n\n// TODO - check if getPixels callback is asynchronous if provided with buffer input\n// if not, parseImage can be a sync function\nfunction getPixelsAsync(buffer: Buffer, mimeType: string): Promise<NDArray> {\n return new Promise<NDArray>((resolve) =>\n getPixels(buffer, mimeType, (err, ndarray) => {\n if (err) {\n throw err;\n }\n\n const shape = [...ndarray.shape];\n const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;\n const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;\n\n // extract width/height etc\n resolve({\n shape,\n data,\n width: ndarray.shape[0],\n height: ndarray.shape[1],\n components: ndarray.shape[2],\n // TODO - error\n layers: layers ? [layers] : []\n });\n })\n );\n}\n"],"mappings":";;;;;;;;;;;AAEA;;AAGO,IAAMA,mBAAmB,GAAG,CAAC,WAAW,EAAE,YAAY,EAAE,WAAW,CAAC;;AAAC;AAAA,SAYtDC,cAAc;EAAA;AAAA;AAAA;EAAA,4EAA7B,iBAA8BC,WAAwB,EAAEC,QAAgB;IAAA;IAAA;MAAA;QAAA;UAAA;YAAA,IACxEA,QAAQ;cAAA;cAAA;YAAA;YAAA,MACL,IAAIC,KAAK,CAAC,mDAAmD,CAAC;UAAA;YAGhEC,MAAM,GAAGH,WAAW,YAAYI,MAAM,GAAGJ,WAAW,GAAGI,MAAM,CAACC,IAAI,CAACL,WAAW,CAAC;YAAA;YAAA,OAC/DM,cAAc,CAACH,MAAM,EAAEF,QAAQ,CAAC;UAAA;YAAhDM,OAAO;YAAA,iCACNA,OAAO;UAAA;UAAA;YAAA;QAAA;MAAA;IAAA;EAAA,CACf;EAAA;AAAA;AAID,SAASD,cAAc,CAACH,MAAc,EAAEF,QAAgB,EAAoB;EAC1E,OAAO,IAAIO,OAAO,CAAU,UAACC,OAAO;IAAA,OAClC,IAAAC,kBAAS,EAACP,MAAM,EAAEF,QAAQ,EAAE,UAACU,GAAG,EAAEJ,OAAO,EAAK;MAC5C,IAAII,GAAG,EAAE;QACP,MAAMA,GAAG;MACX;MAEA,IAAMC,KAAK,oCAAOL,OAAO,CAACK,KAAK,CAAC;MAChC,IAAMC,MAAM,GAAGN,OAAO,CAACK,KAAK,CAACE,MAAM,KAAK,CAAC,GAAGP,OAAO,CAACK,KAAK,CAACG,KAAK,EAAE,GAAG,CAAC;MACrE,IAAMC,IAAI,GAAGT,OAAO,CAACS,IAAI,YAAYZ,MAAM,GAAG,IAAIa,UAAU,CAACV,OAAO,CAACS,IAAI,CAAC,GAAGT,OAAO,CAACS,IAAI;;MAGzFP,OAAO,CAAC;QACNG,KAAK,EAALA,KAAK;QACLI,IAAI,EAAJA,IAAI;QACJE,KAAK,EAAEX,OAAO,CAACK,KAAK,CAAC,CAAC,CAAC;QACvBO,MAAM,EAAEZ,OAAO,CAACK,KAAK,CAAC,CAAC,CAAC;QACxBQ,UAAU,EAAEb,OAAO,CAACK,KAAK,CAAC,CAAC,CAAC;QAE5BC,MAAM,EAAEA,MAAM,GAAG,CAACA,MAAM,CAAC,GAAG;MAC9B,CAAC,CAAC;IACJ,CAAC,CAAC;EAAA,EACH;AACH"}
package/dist/esm/index.js CHANGED
@@ -9,7 +9,7 @@ import { Headers as HeadersNode } from './node/fetch/headers.node';
9
9
  import { Response as ResponseNode } from './node/fetch/response.node';
10
10
  import { fetchNode } from './node/fetch/fetch.node';
11
11
  import { encodeImageNode } from './node/images/encode-image.node';
12
- import { parseImageNode } from './node/images/parse-image.node';
12
+ import { parseImageNode, NODE_FORMAT_SUPPORT } from './node/images/parse-image.node';
13
13
  export { ReadableStreamPolyfill } from './node/file/readable-stream';
14
14
  export { BlobPolyfill } from './node/file/blob';
15
15
  export { FileReaderPolyfill } from './node/file/file-reader';
@@ -53,6 +53,7 @@ if (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {
53
53
  }
54
54
  if (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {
55
55
  global['_parseImageNode'] = parseImageNode;
56
+ global['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;
56
57
  }
57
58
  if (!('allSettled' in Promise)) {
58
59
  Promise.allSettled = allSettled;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["DOMParser","isBrowser","global","TextDecoder","TextEncoder","allSettled","base64","Headers","HeadersNode","Response","ResponseNode","fetchNode","encodeImageNode","parseImageNode","ReadableStreamPolyfill","BlobPolyfill","FileReaderPolyfill","FilePolyfill","installFilePolyfills","_fetchNode","fetchFileNode","_fetchFileNode","installTextEncoder","installTextDecoder","atob","btoa","Promise"],"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable dot-notation */\nimport {DOMParser} from '@xmldom/xmldom';\nimport {isBrowser, global} from './utils/globals';\n\nimport {TextDecoder, TextEncoder} from './lib/encoding';\nimport {allSettled} from './promise/all-settled';\n\n// Node specific\nimport * as base64 from './node/buffer/btoa.node';\n\nimport {Headers as HeadersNode} from './node/fetch/headers.node';\nimport {Response as ResponseNode} from './node/fetch/response.node';\nimport {fetchNode as fetchNode} from './node/fetch/fetch.node';\n\nimport {encodeImageNode} from './node/images/encode-image.node';\nimport {parseImageNode} from './node/images/parse-image.node';\n\nexport {ReadableStreamPolyfill} from './node/file/readable-stream';\nexport {BlobPolyfill} from './node/file/blob';\nexport {FileReaderPolyfill} from './node/file/file-reader';\nexport {FilePolyfill} from './node/file/file';\nexport {installFilePolyfills} from './node/file/install-file-polyfills';\nexport {fetchNode as _fetchNode} from './node/fetch/fetch.node';\nexport {fetchFileNode as _fetchFileNode} from './node/fetch/fetch-file.node';\n\n// POLYFILLS: TextEncoder, TextDecoder\n// - Recent Node versions have these classes but virtually no encodings unless special build.\n// - Browser: Edge, IE11 do not have these\n\nconst installTextEncoder = !isBrowser || !('TextEncoder' in global);\nif (installTextEncoder) {\n global['TextEncoder'] = TextEncoder;\n}\n\nconst installTextDecoder = !isBrowser || !('TextDecoder' in global);\nif (installTextDecoder) {\n global['TextDecoder'] = TextDecoder;\n}\n\n// POLYFILLS: btoa, atob\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('atob' in global) && base64.atob) {\n global['atob'] = base64.atob;\n}\nif (!isBrowser && !('btoa' in global) && base64.btoa) {\n global['btoa'] = base64.btoa;\n}\n\n// POLYFILL: fetch\n// - Node: Yes\n// - Browser: No. For This polyfill is node only, IE11 etc, install external polyfill\n\nif (!isBrowser && !('Headers' in global) && HeadersNode) {\n global['Headers'] = HeadersNode;\n}\n\nif (!isBrowser && !('Response' in global) && ResponseNode) {\n global['Response'] = ResponseNode;\n}\n\nif (!isBrowser && !('fetch' in global) && fetchNode) {\n global['fetch'] = fetchNode;\n}\n\n// POLYFILL: DOMParser\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('DOMParser' in global) && DOMParser) {\n global['DOMParser'] = DOMParser;\n}\n\n// NODE IMAGE FUNCTIONS:\n// These are not official polyfills but used by the @loaders.gl/images module if installed\n// TODO - is there an appropriate Image API we could polyfill using an adapter?\n\nif (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {\n global['_encodeImageNode'] = encodeImageNode;\n}\n\nif (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {\n global['_parseImageNode'] = parseImageNode;\n}\n\nif (!('allSettled' in Promise)) {\n // @ts-ignore\n Promise.allSettled = allSettled;\n}\n"],"mappings":";AACA,SAAQA,SAAS,QAAO,gBAAgB;AACxC,SAAQC,SAAS,EAAEC,MAAM,QAAO,iBAAiB;AAEjD,SAAQC,WAAW,EAAEC,WAAW,QAAO,gBAAgB;AACvD,SAAQC,UAAU,QAAO,uBAAuB;;AAGhD,OAAO,KAAKC,MAAM,MAAM,yBAAyB;AAEjD,SAAQC,OAAO,IAAIC,WAAW,QAAO,2BAA2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AI,EAAE,UAAU,IAAIC,MAAM,CAAC,IAAIQ,YAAY,EAAE;EACzDR,MAAM,CAAC,UAAU,CAAC,GAAGQ,YAAY;AACnC;AAEA,IAAI,CAACT,SAAS,IAAI,EAAE,OAAO,IAAIC,MAAM,CAAC,IAAIS,SAAS,EAAE;EACnDT,MAAM,CAAC,OAAO,CAAC,GAAGS,SAAS;AAC7B;;AAMA,IAAI,CAACV,SAAS,IAAI,EAAE,WAAW,IAAIC,MAAM,CAAC,IAAIF,SAAS,EAAE;EACvDE,MAAM,CAAC,WAAW,CAAC,GAAGF,SAAS;AACjC;;AAMA,IAAI,CAACC,SAAS,IAAI,EAAE,kBAAkB,IAAIC,MAAM,CAAC,IAAIU,eAAe,EAAE;EACpEV,MAAM,CAAC,kBAAkB,CAAC,GAAGU,eAAe;AAC9C;AAEA,IAAI,CAACX,SAAS,IAAI,EAAE,iBAAiB,IAAIC,MAAM,CAAC,IAAIW,cAAc,EAAE;EAClEX,MAAM,CAAC,iBAAiB,CAAC,GAAGW,cAAc;AAC5C;AAEA,IAAI,EAAE,YAAY,IAAIa,OAAO,CAAC,EAAE;EAE9BA,OAAO,CAACrB,UAAU,GAAGA,UAAU;AACjC"}
1
+ {"version":3,"file":"index.js","names":["DOMParser","isBrowser","global","TextDecoder","TextEncoder","allSettled","base64","Headers","HeadersNode","Response","ResponseNode","fetchNode","encodeImageNode","parseImageNode","NODE_FORMAT_SUPPORT","ReadableStreamPolyfill","BlobPolyfill","FileReaderPolyfill","FilePolyfill","installFilePolyfills","_fetchNode","fetchFileNode","_fetchFileNode","installTextEncoder","installTextDecoder","atob","btoa","Promise"],"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable dot-notation */\nimport {DOMParser} from '@xmldom/xmldom';\nimport {isBrowser, global} from './utils/globals';\n\nimport {TextDecoder, TextEncoder} from './lib/encoding';\nimport {allSettled} from './promise/all-settled';\n\n// Node specific\nimport * as base64 from './node/buffer/btoa.node';\n\nimport {Headers as HeadersNode} from './node/fetch/headers.node';\nimport {Response as ResponseNode} from './node/fetch/response.node';\nimport {fetchNode as fetchNode} from './node/fetch/fetch.node';\n\nimport {encodeImageNode} from './node/images/encode-image.node';\nimport {parseImageNode, NODE_FORMAT_SUPPORT} from './node/images/parse-image.node';\n\nexport {ReadableStreamPolyfill} from './node/file/readable-stream';\nexport {BlobPolyfill} from './node/file/blob';\nexport {FileReaderPolyfill} from './node/file/file-reader';\nexport {FilePolyfill} from './node/file/file';\nexport {installFilePolyfills} from './node/file/install-file-polyfills';\nexport {fetchNode as _fetchNode} from './node/fetch/fetch.node';\nexport {fetchFileNode as _fetchFileNode} from './node/fetch/fetch-file.node';\n\n// POLYFILLS: TextEncoder, TextDecoder\n// - Recent Node versions have these classes but virtually no encodings unless special build.\n// - Browser: Edge, IE11 do not have these\n\nconst installTextEncoder = !isBrowser || !('TextEncoder' in global);\nif (installTextEncoder) {\n global['TextEncoder'] = TextEncoder;\n}\n\nconst installTextDecoder = !isBrowser || !('TextDecoder' in global);\nif (installTextDecoder) {\n global['TextDecoder'] = TextDecoder;\n}\n\n// POLYFILLS: btoa, atob\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('atob' in global) && base64.atob) {\n global['atob'] = base64.atob;\n}\nif (!isBrowser && !('btoa' in global) && base64.btoa) {\n global['btoa'] = base64.btoa;\n}\n\n// POLYFILL: fetch\n// - Node: Yes\n// - Browser: No. For This polyfill is node only, IE11 etc, install external polyfill\n\nif (!isBrowser && !('Headers' in global) && HeadersNode) {\n global['Headers'] = HeadersNode;\n}\n\nif (!isBrowser && !('Response' in global) && ResponseNode) {\n global['Response'] = ResponseNode;\n}\n\nif (!isBrowser && !('fetch' in global) && fetchNode) {\n global['fetch'] = fetchNode;\n}\n\n// POLYFILL: DOMParser\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('DOMParser' in global) && DOMParser) {\n global['DOMParser'] = DOMParser;\n}\n\n// NODE IMAGE FUNCTIONS:\n// These are not official polyfills but used by the @loaders.gl/images module if installed\n// TODO - is there an appropriate Image API we could polyfill using an adapter?\n\nif (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {\n global['_encodeImageNode'] = encodeImageNode;\n}\n\nif (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {\n global['_parseImageNode'] = parseImageNode;\n global['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;\n}\n\nif (!('allSettled' in Promise)) {\n // @ts-ignore\n Promise.allSettled = allSettled;\n}\n"],"mappings":";AACA,SAAQA,SAAS,QAAO,gBAAgB;AACxC,SAAQC,SAAS,EAAEC,MAAM,QAAO,iBAAiB;AAEjD,SAAQC,WAAW,EAAEC,WAAW,QAAO,gBAAgB;AACvD,SAAQC,UAAU,QAAO,uBAAuB;;AAGhD,OAAO,KAAKC,MAAM,MAAM,yBAAyB;AAEjD,SAAQC,OAAO,IAAIC,WAAW,QAAO,2BAA2B;AAChE,SAAQC,QAAQ,IAAIC,YAAY,QAAO,4BAA4B;AACnE,SAAQC,SAAsB,QAAO,yBAAyB;AAE9D,SAAQC,eAAe,QAAO,iCAAiC;AAC/D,SAAQC,cAAc,EAAEC,mBAAmB,QAAO,gCAAgC;AAElF,SAAQC,sBAAsB,QAAO,6BAA6B;AAClE,SAAQC,YAAY,QAAO,kBAAkB;AAC7C,SAAQC,kBAAkB,QAAO,yBAAyB;AAC1D,SAAQC,YAAY,QAAO,kBAAkB;AAC7C,SAAQC,oBAAoB,QAAO,oCAAoC;AACvE,SAAQR,SAAS,IAAIS,UAAU,QAAO,yBAAyB;AAC/D,SAAQC,aAAa,IAAIC,cAAc,QAAO,8BAA8B;;AAM5E,MAAMC,kBAAkB,GAAG,CAACtB,SAAS,IAAI,EAAE,aAAa,IAAIC,MAAM,CAAC;AACnE,IAAIqB,kBAAkB,EAAE;EACtBrB,MAAM,CAAC,aAAa,CAAC,GAAGE,WAAW;AACrC;AAEA,MAAMoB,kBAAkB,GAAG,CAACvB,SAAS,IAAI,EAAE,aAAa,IAAIC,MAAM,C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,CAAC,GAAGY,mBAAmB;AACnD;AAEA,IAAI,EAAE,YAAY,IAAIa,OAAO,CAAC,EAAE;EAE9BA,OAAO,CAACtB,UAAU,GAAGA,UAAU;AACjC"}
@@ -1,24 +1,35 @@
1
+
2
+
1
3
  import getPixels from 'get-pixels';
2
- import { assert } from '../../utils/assert';
3
- import util from 'util';
4
4
 
5
- export async function parseImageNode(arrayBuffer, mimeType) {
6
- assert(mimeType, 'MIMEType is required to parse image under Node.js');
5
+ export const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
7
6
 
8
- const getPixelsAsync = util.promisify(getPixels);
7
+ export async function parseImageNode(arrayBuffer, mimeType) {
8
+ if (!mimeType) {
9
+ throw new Error('MIMEType is required to parse image under Node.js');
10
+ }
9
11
  const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
10
12
  const ndarray = await getPixelsAsync(buffer, mimeType);
11
- const shape = [...ndarray.shape];
12
- const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
13
- const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
13
+ return ndarray;
14
+ }
15
+
16
+ function getPixelsAsync(buffer, mimeType) {
17
+ return new Promise(resolve => getPixels(buffer, mimeType, (err, ndarray) => {
18
+ if (err) {
19
+ throw err;
20
+ }
21
+ const shape = [...ndarray.shape];
22
+ const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
23
+ const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
14
24
 
15
- return {
16
- shape,
17
- data,
18
- width: ndarray.shape[0],
19
- height: ndarray.shape[1],
20
- components: ndarray.shape[2],
21
- layers
22
- };
25
+ resolve({
26
+ shape,
27
+ data,
28
+ width: ndarray.shape[0],
29
+ height: ndarray.shape[1],
30
+ components: ndarray.shape[2],
31
+ layers: layers ? [layers] : []
32
+ });
33
+ }));
23
34
  }
24
35
  //# sourceMappingURL=parse-image.node.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-image.node.js","names":["getPixels","assert","util","parseImageNode","arrayBuffer","mimeType","getPixelsAsync","promisify","buffer","Buffer","from","ndarray","shape","layers","length","shift","data","Uint8Array","width","height","components"],"sources":["../../../../src/node/images/parse-image.node.ts"],"sourcesContent":["import getPixels from 'get-pixels';\nimport {assert} from '../../utils/assert';\nimport util from 'util';\n\n// Note: These types are also defined in @loaders.gl/images and need to be kept in sync\ntype NDArray = {\n shape: number[];\n data: Uint8Array;\n width: number;\n height: number;\n components: number;\n layers: number[];\n};\n\nexport async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {\n assert(mimeType, 'MIMEType is required to parse image under Node.js');\n\n // TODO - check if getPixels callback is asynchronous if provided with buffer input\n // if not, parseImage can be a sync function\n const getPixelsAsync = util.promisify(getPixels);\n\n const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);\n\n const ndarray = await getPixelsAsync(buffer, mimeType);\n\n const shape = [...ndarray.shape];\n const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;\n const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;\n\n // extract width/height etc\n return {\n shape,\n data,\n width: ndarray.shape[0],\n height: ndarray.shape[1],\n components: ndarray.shape[2],\n layers\n };\n}\n"],"mappings":"AAAA,OAAOA,SAAS,MAAM,YAAY;AAClC,SAAQC,MAAM,QAAO,oBAAoB;AACzC,OAAOC,IAAI,MAAM,MAAM;;AAYvB,OAAO,eAAeC,cAAc,CAACC,WAAwB,EAAEC,QAAgB,EAAoB;EACjGJ,MAAM,CAACI,QAAQ,EAAE,mDAAmD,CAAC;;EAIrE,MAAMC,cAAc,GAAGJ,IAAI,CAACK,SAAS,CAACP,SAAS,CAAC;EAEhD,MAAMQ,MAAM,GAAGJ,WAAW,YAAYK,MAAM,GAAGL,WAAW,GAAGK,MAAM,CAACC,IAAI,CAACN,WAAW,CAAC;EAErF,MAAMO,OAAO,GAAG,MAAML,cAAc,CAACE,MAAM,EAAEH,QAAQ,CAAC;EAEtD,MAAMO,KAAK,GAAG,CAAC,GAAGD,OAAO,CAACC,KAAK,CAAC;EAChC,MAAMC,MAAM,GAAGF,OAAO,CAACC,KAAK,CAACE,MAAM,KAAK,CAAC,GAAGH,OAAO,CAACC,KAAK,CAACG,KAAK,EAAE,GAAG,CAAC;EACrE,MAAMC,IAAI,GAAGL,OAAO,CAACK,IAAI,YAAYP,MAAM,GAAG,IAAIQ,UAAU,CAACN,OAAO,CAACK,IAAI,CAAC,GAAGL,OAAO,CAACK,IAAI;;EAGzF,OAAO;IACLJ,KAAK;IACLI,IAAI;IACJE,KAAK,EAAEP,OAAO,CAACC,KAAK,CAAC,CAAC,CAAC;IACvBO,MAAM,EAAER,OAAO,CAACC,KAAK,CAAC,CAAC,CAAC;IACxBQ,UAAU,EAAET,OAAO,CAACC,KAAK,CAAC,CAAC,CAAC;IAC5BC;EACF,CAAC;AACH"}
1
+ {"version":3,"file":"parse-image.node.js","names":["getPixels","NODE_FORMAT_SUPPORT","parseImageNode","arrayBuffer","mimeType","Error","buffer","Buffer","from","ndarray","getPixelsAsync","Promise","resolve","err","shape","layers","length","shift","data","Uint8Array","width","height","components"],"sources":["../../../../src/node/images/parse-image.node.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport getPixels from 'get-pixels';\n\n/** Declares which image format mime types this loader polyfill supports */\nexport const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];\n\n// Note: These types are also defined in @loaders.gl/images and need to be kept in sync\ntype NDArray = {\n shape: number[];\n data: Uint8Array;\n width: number;\n height: number;\n components: number;\n layers: number[];\n};\n\nexport async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {\n if (!mimeType) {\n throw new Error('MIMEType is required to parse image under Node.js');\n }\n\n const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);\n const ndarray = await getPixelsAsync(buffer, mimeType);\n return ndarray;\n}\n\n// TODO - check if getPixels callback is asynchronous if provided with buffer input\n// if not, parseImage can be a sync function\nfunction getPixelsAsync(buffer: Buffer, mimeType: string): Promise<NDArray> {\n return new Promise<NDArray>((resolve) =>\n getPixels(buffer, mimeType, (err, ndarray) => {\n if (err) {\n throw err;\n }\n\n const shape = [...ndarray.shape];\n const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;\n const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;\n\n // extract width/height etc\n resolve({\n shape,\n data,\n width: ndarray.shape[0],\n height: ndarray.shape[1],\n components: ndarray.shape[2],\n // TODO - error\n layers: layers ? [layers] : []\n });\n })\n );\n}\n"],"mappings":";;AAEA,OAAOA,SAAS,MAAM,YAAY;;AAGlC,OAAO,MAAMC,mBAAmB,GAAG,CAAC,WAAW,EAAE,YAAY,EAAE,WAAW,CAAC;;AAY3E,OAAO,eAAeC,cAAc,CAACC,WAAwB,EAAEC,QAAgB,EAAoB;EACjG,IAAI,CAACA,QAAQ,EAAE;IACb,MAAM,IAAIC,KAAK,CAAC,mDAAmD,CAAC;EACtE;EAEA,MAAMC,MAAM,GAAGH,WAAW,YAAYI,MAAM,GAAGJ,WAAW,GAAGI,MAAM,CAACC,IAAI,CAACL,WAAW,CAAC;EACrF,MAAMM,OAAO,GAAG,MAAMC,cAAc,CAACJ,MAAM,EAAEF,QAAQ,CAAC;EACtD,OAAOK,OAAO;AAChB;;AAIA,SAASC,cAAc,CAACJ,MAAc,EAAEF,QAAgB,EAAoB;EAC1E,OAAO,IAAIO,OAAO,CAAWC,OAAO,IAClCZ,SAAS,CAACM,MAAM,EAAEF,QAAQ,EAAE,CAACS,GAAG,EAAEJ,OAAO,KAAK;IAC5C,IAAII,GAAG,EAAE;MACP,MAAMA,GAAG;IACX;IAEA,MAAMC,KAAK,GAAG,CAAC,GAAGL,OAAO,CAACK,KAAK,CAAC;IAChC,MAAMC,MAAM,GAAGN,OAAO,CAACK,KAAK,CAACE,MAAM,KAAK,CAAC,GAAGP,OAAO,CAACK,KAAK,CAACG,KAAK,EAAE,GAAG,CAAC;IACrE,MAAMC,IAAI,GAAGT,OAAO,CAACS,IAAI,YAAYX,MAAM,GAAG,IAAIY,UAAU,CAACV,OAAO,CAACS,IAAI,CAAC,GAAGT,OAAO,CAACS,IAAI;;IAGzFN,OAAO,CAAC;MACNE,KAAK;MACLI,IAAI;MACJE,KAAK,EAAEX,OAAO,CAACK,KAAK,CAAC,CAAC,CAAC;MACvBO,MAAM,EAAEZ,OAAO,CAACK,KAAK,CAAC,CAAC,CAAC;MACxBQ,UAAU,EAAEb,OAAO,CAACK,KAAK,CAAC,CAAC,CAAC;MAE5BC,MAAM,EAAEA,MAAM,GAAG,CAACA,MAAM,CAAC,GAAG;IAC9B,CAAC,CAAC;EACJ,CAAC,CAAC,CACH;AACH"}
package/dist/index.js CHANGED
@@ -96,6 +96,7 @@ if (!globals_1.isBrowser && !('_encodeImageNode' in globals_1.global) && encode_
96
96
  }
97
97
  if (!globals_1.isBrowser && !('_parseImageNode' in globals_1.global) && parse_image_node_1.parseImageNode) {
98
98
  globals_1.global['_parseImageNode'] = parse_image_node_1.parseImageNode;
99
+ globals_1.global['_imageFormatsNode'] = parse_image_node_1.NODE_FORMAT_SUPPORT;
99
100
  }
100
101
  if (!('allSettled' in Promise)) {
101
102
  // @ts-ignore
@@ -1,3 +1,5 @@
1
+ /** Declares which image format mime types this loader polyfill supports */
2
+ export declare const NODE_FORMAT_SUPPORT: string[];
1
3
  type NDArray = {
2
4
  shape: number[];
3
5
  data: Uint8Array;
@@ -1 +1 @@
1
- {"version":3,"file":"parse-image.node.d.ts","sourceRoot":"","sources":["../../../src/node/images/parse-image.node.ts"],"names":[],"mappings":"AAKA,KAAK,OAAO,GAAG;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF,wBAAsB,cAAc,CAAC,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAwBjG"}
1
+ {"version":3,"file":"parse-image.node.d.ts","sourceRoot":"","sources":["../../../src/node/images/parse-image.node.ts"],"names":[],"mappings":"AAIA,2EAA2E;AAC3E,eAAO,MAAM,mBAAmB,UAA2C,CAAC;AAG5E,KAAK,OAAO,GAAG;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF,wBAAsB,cAAc,CAAC,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAQjG"}
@@ -1,30 +1,41 @@
1
1
  "use strict";
2
+ // loaders.gl, MIT license
2
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
5
  };
5
6
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseImageNode = void 0;
7
+ exports.parseImageNode = exports.NODE_FORMAT_SUPPORT = void 0;
7
8
  const get_pixels_1 = __importDefault(require("get-pixels"));
8
- const assert_1 = require("../../utils/assert");
9
- const util_1 = __importDefault(require("util"));
9
+ /** Declares which image format mime types this loader polyfill supports */
10
+ exports.NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
10
11
  async function parseImageNode(arrayBuffer, mimeType) {
11
- (0, assert_1.assert)(mimeType, 'MIMEType is required to parse image under Node.js');
12
- // TODO - check if getPixels callback is asynchronous if provided with buffer input
13
- // if not, parseImage can be a sync function
14
- const getPixelsAsync = util_1.default.promisify(get_pixels_1.default);
12
+ if (!mimeType) {
13
+ throw new Error('MIMEType is required to parse image under Node.js');
14
+ }
15
15
  const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
16
16
  const ndarray = await getPixelsAsync(buffer, mimeType);
17
- const shape = [...ndarray.shape];
18
- const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
19
- const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
20
- // extract width/height etc
21
- return {
22
- shape,
23
- data,
24
- width: ndarray.shape[0],
25
- height: ndarray.shape[1],
26
- components: ndarray.shape[2],
27
- layers
28
- };
17
+ return ndarray;
29
18
  }
30
19
  exports.parseImageNode = parseImageNode;
20
+ // TODO - check if getPixels callback is asynchronous if provided with buffer input
21
+ // if not, parseImage can be a sync function
22
+ function getPixelsAsync(buffer, mimeType) {
23
+ return new Promise((resolve) => (0, get_pixels_1.default)(buffer, mimeType, (err, ndarray) => {
24
+ if (err) {
25
+ throw err;
26
+ }
27
+ const shape = [...ndarray.shape];
28
+ const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
29
+ const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
30
+ // extract width/height etc
31
+ resolve({
32
+ shape,
33
+ data,
34
+ width: ndarray.shape[0],
35
+ height: ndarray.shape[1],
36
+ components: ndarray.shape[2],
37
+ // TODO - error
38
+ layers: layers ? [layers] : []
39
+ });
40
+ }));
41
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/polyfills",
3
- "version": "3.4.0-alpha.1",
3
+ "version": "3.4.0-alpha.2",
4
4
  "description": "Polyfills for TextEncoder/TextDecoder",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -106,5 +106,5 @@
106
106
  "through": "^2.3.8",
107
107
  "web-streams-polyfill": "^3.0.0"
108
108
  },
109
- "gitHead": "4085b0323050e4361614471319a1fb4729547bbf"
109
+ "gitHead": "f1c00c124d8d0c41a138ff40afb0d1a00711bf2e"
110
110
  }
package/src/index.ts CHANGED
@@ -13,7 +13,7 @@ import {Response as ResponseNode} from './node/fetch/response.node';
13
13
  import {fetchNode as fetchNode} from './node/fetch/fetch.node';
14
14
 
15
15
  import {encodeImageNode} from './node/images/encode-image.node';
16
- import {parseImageNode} from './node/images/parse-image.node';
16
+ import {parseImageNode, NODE_FORMAT_SUPPORT} from './node/images/parse-image.node';
17
17
 
18
18
  export {ReadableStreamPolyfill} from './node/file/readable-stream';
19
19
  export {BlobPolyfill} from './node/file/blob';
@@ -82,6 +82,7 @@ if (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {
82
82
 
83
83
  if (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {
84
84
  global['_parseImageNode'] = parseImageNode;
85
+ global['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;
85
86
  }
86
87
 
87
88
  if (!('allSettled' in Promise)) {
@@ -1,6 +1,9 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  import getPixels from 'get-pixels';
2
- import {assert} from '../../utils/assert';
3
- import util from 'util';
4
+
5
+ /** Declares which image format mime types this loader polyfill supports */
6
+ export const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
4
7
 
5
8
  // Note: These types are also defined in @loaders.gl/images and need to be kept in sync
6
9
  type NDArray = {
@@ -13,27 +16,38 @@ type NDArray = {
13
16
  };
14
17
 
15
18
  export async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {
16
- assert(mimeType, 'MIMEType is required to parse image under Node.js');
17
-
18
- // TODO - check if getPixels callback is asynchronous if provided with buffer input
19
- // if not, parseImage can be a sync function
20
- const getPixelsAsync = util.promisify(getPixels);
19
+ if (!mimeType) {
20
+ throw new Error('MIMEType is required to parse image under Node.js');
21
+ }
21
22
 
22
23
  const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
23
-
24
24
  const ndarray = await getPixelsAsync(buffer, mimeType);
25
+ return ndarray;
26
+ }
27
+
28
+ // TODO - check if getPixels callback is asynchronous if provided with buffer input
29
+ // if not, parseImage can be a sync function
30
+ function getPixelsAsync(buffer: Buffer, mimeType: string): Promise<NDArray> {
31
+ return new Promise<NDArray>((resolve) =>
32
+ getPixels(buffer, mimeType, (err, ndarray) => {
33
+ if (err) {
34
+ throw err;
35
+ }
36
+
37
+ const shape = [...ndarray.shape];
38
+ const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
39
+ const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
25
40
 
26
- const shape = [...ndarray.shape];
27
- const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
28
- const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
29
-
30
- // extract width/height etc
31
- return {
32
- shape,
33
- data,
34
- width: ndarray.shape[0],
35
- height: ndarray.shape[1],
36
- components: ndarray.shape[2],
37
- layers
38
- };
41
+ // extract width/height etc
42
+ resolve({
43
+ shape,
44
+ data,
45
+ width: ndarray.shape[0],
46
+ height: ndarray.shape[1],
47
+ components: ndarray.shape[2],
48
+ // TODO - error
49
+ layers: layers ? [layers] : []
50
+ });
51
+ })
52
+ );
39
53
  }