Skip to content

Commit 25bb97e

Browse files
committed
Refactor and resolve linter violations
1 parent 2823d83 commit 25bb97e

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

index.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,12 @@ describe("Replicate client", () => {
219219
])(
220220
"converts a $type input into a base64 encoded string",
221221
async ({ value: data, expected }) => {
222-
let body: Record<string, any>;
222+
let actual: Record<string, any> | undefined;
223223
nock(BASE_URL)
224224
.post("/predictions")
225-
.reply(201, (_uri, _body: Record<string, any>) => {
226-
return (body = _body);
225+
.reply(201, (uri: string, body: Record<string, any>) => {
226+
actual = body;
227+
return body;
227228
});
228229

229230
await client.predictions.create({
@@ -236,7 +237,7 @@ describe("Replicate client", () => {
236237
stream: true,
237238
});
238239

239-
expect(body!.input.data).toEqual(expected);
240+
expect(actual?.input.data).toEqual(expected);
240241
}
241242
);
242243

lib/util.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,52 +72,53 @@ const MAX_DATA_URI_SIZE = 10_000_000;
7272

7373
/**
7474
* Walks the inputs and transforms any binary data found into a
75-
* base64 encoded data uri. It will throw if the size of inputs
76-
* exceeds a given threshould set by MAX_DATA_URI_SIZE.
75+
* base64-encoded data URI.
76+
*
77+
* @param {object} inputs - The inputs to transform
78+
* @returns {object} - The transformed inputs
79+
* @throws {Error} If the size of inputs exceeds a given threshould set by MAX_DATA_URI_SIZE
7780
*/
7881
async function transformFileInputs(inputs) {
7982
let totalBytes = 0;
8083
const result = await transform(inputs, async (value) => {
84+
let buffer;
8185
let mime;
8286

83-
// Currently we use a NodeJS only API for base64 encoding, as
84-
// we move to support the browser we could support either using
85-
// btoa (which does string encoding), the FileReader API or
86-
// a JavaScript implenentation like base64-js.
87-
// See: https://developer.mozilla.org/en-US/docs/Glossary/Base64
88-
// See: https://github.com/beatgammit/base64-js
8987
if (value instanceof Blob) {
88+
// Currently we use a NodeJS only API for base64 encoding, as
89+
// we move to support the browser we could support either using
90+
// btoa (which does string encoding), the FileReader API or
91+
// a JavaScript implenentation like base64-js.
92+
// See: https://developer.mozilla.org/en-US/docs/Glossary/Base64
93+
// See: https://github.com/beatgammit/base64-js
94+
buffer = Buffer.from(await value.arrayBuffer());
9095
mime = value.type;
91-
value = Buffer.from(await value.arrayBuffer());
92-
}
93-
94-
if (!Buffer.isBuffer(value)) {
96+
} else if (Buffer.isBuffer(value)) {
97+
buffer = value;
98+
} else {
9599
return value;
96100
}
97101

98-
totalBytes = 0 + value.byteLength;
102+
totalBytes += buffer.byteLength;
99103
if (totalBytes > MAX_DATA_URI_SIZE) {
100-
return null;
104+
throw new Error(
105+
`Combined filesize of prediction ${totalBytes} bytes exceeds 10mb limit for inline encoding, please provide URLs instead`
106+
);
101107
}
102108

103-
const data = value.toString("base64");
109+
const data = buffer.toString("base64");
104110
mime = mime ?? "application/octet-stream";
111+
105112
return `data:${mime};base64,${data}`;
106113
});
107114

108-
if (totalBytes > MAX_DATA_URI_SIZE) {
109-
throw new Error(
110-
`Combined filesize of prediction ${totalBytes} bytes exceeds 10mb limit for inline encoding, please provide URLs instead`
111-
);
112-
}
113-
114115
return result;
115116
}
116117

117118
// Walk a JavaScript object and transform the leaf values.
118119
async function transform(value, mapper) {
119120
if (Array.isArray(value)) {
120-
const copy = [];
121+
let copy = [];
121122
for (const val of value) {
122123
copy = await transform(val, mapper);
123124
}
@@ -138,21 +139,21 @@ async function transform(value, mapper) {
138139
// Test for a plain JS object.
139140
// Source: lodash.isPlainObject
140141
function isPlainObject(value) {
141-
const isObjectLike = typeof value == "object" && value !== null;
142+
const isObjectLike = typeof value === "object" && value !== null;
142143
if (!isObjectLike || String(value) !== "[object Object]") {
143144
return false;
144145
}
145-
var proto = Object.getPrototypeOf(value);
146+
const proto = Object.getPrototypeOf(value);
146147
if (proto === null) {
147148
return true;
148149
}
149-
var Ctor =
150+
const Ctor =
150151
Object.prototype.hasOwnProperty.call(proto, "constructor") &&
151152
proto.constructor;
152153
return (
153-
typeof Ctor == "function" &&
154+
typeof Ctor === "function" &&
154155
Ctor instanceof Ctor &&
155-
Function.prototype.toString.call(Ctor) ==
156+
Function.prototype.toString.call(Ctor) ===
156157
Function.prototype.toString.call(Object)
157158
);
158159
}

0 commit comments

Comments
 (0)