UNPKG

1.92 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2017,2019. All Rights Reserved.
3// Node module: @loopback/repository
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.BufferType = void 0;
8const tslib_1 = require("tslib");
9const util_1 = tslib_1.__importDefault(require("util"));
10/* eslint-disable @typescript-eslint/no-explicit-any */
11/**
12 * Buffer (binary) type
13 */
14class BufferType {
15 constructor() {
16 this.name = 'buffer';
17 }
18 isInstance(value) {
19 return value == null || Buffer.isBuffer(value);
20 }
21 defaultValue() {
22 return Buffer.from([]);
23 }
24 isCoercible(value) {
25 if (value == null)
26 return true;
27 if (typeof value === 'string')
28 return true;
29 if (Buffer.isBuffer(value))
30 return true;
31 if (Array.isArray(value))
32 return true;
33 return false;
34 }
35 coerce(value, options) {
36 if (value == null)
37 return value;
38 if (Buffer.isBuffer(value))
39 return value;
40 if (typeof value === 'string') {
41 options = options !== null && options !== void 0 ? options : {};
42 const encoding = options.encoding || 'utf-8';
43 return Buffer.from(value, encoding);
44 }
45 else if (Array.isArray(value)) {
46 return Buffer.from(value);
47 }
48 const msg = util_1.default.format('Invalid %s: %j', this.name, value);
49 throw new TypeError(msg);
50 }
51 serialize(value, options) {
52 if (value == null)
53 return value;
54 const encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'base64';
55 return value.toString(encoding);
56 }
57}
58exports.BufferType = BufferType;
59//# sourceMappingURL=buffer.js.map
\No newline at end of file