Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 27, 2023
1 parent 51662a2 commit 7541964
Show file tree
Hide file tree
Showing 24 changed files with 1,200 additions and 7,673 deletions.
4 changes: 2 additions & 2 deletions packages/data-uri-to-buffer/src/index.ts
Expand Up @@ -37,8 +37,8 @@ export function dataUriToBuffer(uri: string): MimeBuffer {
for (let i = 1; i < meta.length; i++) {
if (meta[i] === 'base64') {
base64 = true;
} else if(meta[i]) {
typeFull += `;${ meta[i]}`;
} else if (meta[i]) {
typeFull += `;${meta[i]}`;
if (meta[i].indexOf('charset=') === 0) {
charset = meta[i].substring(8);
}
Expand Down
36 changes: 18 additions & 18 deletions packages/data-uri-to-buffer/test/data-uri-to-buffer.test.ts
@@ -1,8 +1,8 @@
import assert from 'assert';
import dataUriToBuffer from '../src';

describe('data-uri-to-buffer', function() {
it('should decode bare-bones Data URIs', function() {
describe('data-uri-to-buffer', function () {
it('should decode bare-bones Data URIs', function () {
const uri = 'data:,Hello%2C%20World!';

const buf = dataUriToBuffer(uri);
Expand All @@ -12,15 +12,15 @@ describe('data-uri-to-buffer', function() {
assert.equal('Hello, World!', buf.toString());
});

it('should decode bare-bones "base64" Data URIs', function() {
it('should decode bare-bones "base64" Data URIs', function () {
const uri = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D';

const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
assert.equal('Hello, World!', buf.toString());
});

it('should decode plain-text Data URIs', function() {
it('should decode plain-text Data URIs', function () {
const html =
'<!DOCTYPE html>' +
'<html lang="en">' +
Expand All @@ -40,7 +40,7 @@ describe('data-uri-to-buffer', function() {
// the next 4 tests are from:
// https://bug161965.bugzilla.mozilla.org/attachment.cgi?id=94670&action=view

it('should decode "ISO-8859-8 in Base64" URIs', function() {
it('should decode "ISO-8859-8 in Base64" URIs', function () {
const uri = 'data:text/plain;charset=iso-8859-8-i;base64,+ezl7Q==';

const buf = dataUriToBuffer(uri);
Expand All @@ -53,7 +53,7 @@ describe('data-uri-to-buffer', function() {
assert.equal(0xed, buf[3]);
});

it('should decode "ISO-8859-8 in URL-encoding" URIs', function() {
it('should decode "ISO-8859-8 in URL-encoding" URIs', function () {
const uri = 'data:text/plain;charset=iso-8859-8-i,%f9%ec%e5%ed';

const buf = dataUriToBuffer(uri);
Expand All @@ -66,7 +66,7 @@ describe('data-uri-to-buffer', function() {
assert.equal(0xed, buf[3]);
});

it('should decode "UTF-8 in Base64" URIs', function() {
it('should decode "UTF-8 in Base64" URIs', function () {
const uri = 'data:text/plain;charset=UTF-8;base64,16nXnNeV150=';

const buf = dataUriToBuffer(uri);
Expand All @@ -76,7 +76,7 @@ describe('data-uri-to-buffer', function() {
assert.equal('שלום', buf.toString('utf8'));
});

it('should decode "UTF-8 in URL-encoding" URIs', function() {
it('should decode "UTF-8 in URL-encoding" URIs', function () {
const uri = 'data:text/plain;charset=UTF-8,%d7%a9%d7%9c%d7%95%d7%9d';

const buf = dataUriToBuffer(uri);
Expand All @@ -88,7 +88,7 @@ describe('data-uri-to-buffer', function() {

// this next one is from Wikipedia IIRC

it('should decode "base64" Data URIs with newlines', function() {
it('should decode "base64" Data URIs with newlines', function () {
const uri =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA\n' +
'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO\n' +
Expand All @@ -104,29 +104,29 @@ describe('data-uri-to-buffer', function() {
);
});

it('should decode a plain-text URI with a space character in it', function() {
it('should decode a plain-text URI with a space character in it', function () {
const uri = 'data:,foo bar';

const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
assert.equal('foo bar', buf.toString());
});

it('should decode data with a "," comma char', function() {
it('should decode data with a "," comma char', function () {
const uri = 'data:,a,b';
const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
assert.equal('a,b', buf.toString());
});

it('should decode data with traditionally reserved characters like ";"', function() {
it('should decode data with traditionally reserved characters like ";"', function () {
const uri = 'data:,;test';
const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
assert.equal(';test', buf.toString());
});

it('should not default to US-ASCII if main type is provided', function() {
it('should not default to US-ASCII if main type is provided', function () {
const uri = 'data:text/plain,abc';
const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
Expand All @@ -135,7 +135,7 @@ describe('data-uri-to-buffer', function() {
assert.equal('abc', buf.toString());
});

it('should default to text/plain if main type is not provided', function() {
it('should default to text/plain if main type is not provided', function () {
const uri = 'data:;charset=UTF-8,abc';
const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
Expand All @@ -144,7 +144,7 @@ describe('data-uri-to-buffer', function() {
assert.equal('abc', buf.toString());
});

it('should not allow charset without a leading ;', function() {
it('should not allow charset without a leading ;', function () {
const uri = 'data:charset=UTF-8,abc';
const buf = dataUriToBuffer(uri);
assert.equal('charset=UTF-8', buf.type);
Expand All @@ -153,7 +153,7 @@ describe('data-uri-to-buffer', function() {
assert.equal('abc', buf.toString());
});

it('should allow custom media type parameters', function() {
it('should allow custom media type parameters', function () {
const uri = 'data:application/javascript;version=1.8;charset=UTF-8,abc';
const buf = dataUriToBuffer(uri);
assert.equal('application/javascript', buf.type);
Expand All @@ -165,7 +165,7 @@ describe('data-uri-to-buffer', function() {
assert.equal('abc', buf.toString());
});

it('should allow base64 annotation anywhere', function() {
it('should allow base64 annotation anywhere', function () {
const uri = 'data:text/plain;base64;charset=UTF-8,YWJj';
const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
Expand All @@ -174,7 +174,7 @@ describe('data-uri-to-buffer', function() {
assert.equal('abc', buf.toString());
});

it('should parse meta with unnecessary semicolons', function() {
it('should parse meta with unnecessary semicolons', function () {
const uri = 'data:text/plain;;charset=UTF-8;;;base64;;;;,YWJj';
const buf = dataUriToBuffer(uri);
assert.equal('text/plain', buf.type);
Expand Down
79 changes: 38 additions & 41 deletions packages/degenerator/src/index.ts
Expand Up @@ -14,10 +14,7 @@ import { VM, VMScript } from 'vm2';
* @api public
*/

export function degenerator(
code: string,
_names: DegeneratorNames
): string {
export function degenerator(code: string, _names: DegeneratorNames): string {
if (!Array.isArray(_names)) {
throw new TypeError('an array of async function "names" is required');
}
Expand Down Expand Up @@ -124,45 +121,45 @@ export function degenerator(
return generate(ast);
}

export type DegeneratorName = string | RegExp;
export type DegeneratorNames = DegeneratorName[];
export interface CompileOptions extends RunningScriptOptions {
sandbox?: Context;
export type DegeneratorName = string | RegExp;
export type DegeneratorNames = DegeneratorName[];
export interface CompileOptions extends RunningScriptOptions {
sandbox?: Context;
}
export function compile<R = unknown, A extends unknown[] = []>(
code: string,
returnName: string,
names: DegeneratorNames,
options: CompileOptions = {}
): (...args: A) => Promise<R> {
const compiled = degenerator(code, names);
const vm = new VM(options);
const script = new VMScript(`${compiled};${returnName}`, {
filename: options.filename,
});
const fn = vm.run(script);
if (typeof fn !== 'function') {
throw new Error(
`Expected a "function" to be returned for \`${returnName}\`, but got "${typeof fn}"`
);
}
export function compile<R = unknown, A extends unknown[] = []>(
code: string,
returnName: string,
names: DegeneratorNames,
options: CompileOptions = {}
): (...args: A) => Promise<R> {
const compiled = degenerator(code, names);
const vm = new VM(options);
const script = new VMScript(`${compiled};${returnName}`, {
filename: options.filename,
});
const fn = vm.run(script);
if (typeof fn !== "function") {
throw new Error(
`Expected a "function" to be returned for \`${returnName}\`, but got "${typeof fn}"`
);
}
const r = function (this: unknown, ...args: A): Promise<R> {
try {
const p = fn.apply(this, args);
if (typeof p?.then === "function") {
return p;
}
return Promise.resolve(p);
} catch (err) {
return Promise.reject(err);
const r = function (this: unknown, ...args: A): Promise<R> {
try {
const p = fn.apply(this, args);
if (typeof p?.then === 'function') {
return p;
}
};
Object.defineProperty(r, "toString", {
value: fn.toString.bind(fn),
enumerable: false,
});
return r;
}
return Promise.resolve(p);
} catch (err) {
return Promise.reject(err);
}
};
Object.defineProperty(r, 'toString', {
value: fn.toString.bind(fn),
enumerable: false,
});
return r;
}

/**
* Returns `true` if `node` has a matching name to one of the entries in the
Expand Down
1 change: 1 addition & 0 deletions packages/get-uri/.eslintignore
@@ -0,0 +1 @@
dist
6 changes: 3 additions & 3 deletions packages/get-uri/src/file.ts
Expand Up @@ -55,16 +55,16 @@ export const file: GetUriProtocol<FileOptions> = async (

// `fs.ReadStream` takes care of calling `fs.close()` on the
// fd after it's done reading
// @ts-ignore - `@types/node` doesn't allow `null` as file path :/
// @ts-expect-error `@types/node` doesn't allow `null` as file path :/
const rs = createReadStream(null, {
autoClose: true,
...opts,
fd,
}) as FileReadable;
rs.stat = stat;
return rs;
} catch (err: any) {
if (err.code === 'ENOENT') {
} catch (err: unknown) {
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
throw new NotFoundError();
}
throw err;
Expand Down
4 changes: 2 additions & 2 deletions packages/get-uri/src/ftp.ts
Expand Up @@ -48,9 +48,9 @@ export const ftp: GetUriProtocol<FTPOptions> = async (url, opts = {}) => {
// try the MDTM command first, which is an optional extension command.
try {
lastModified = await client.lastMod(filepath);
} catch (err: any) {
} catch (err: unknown) {
// handle the "file not found" error code
if (err.code === 550) {
if ((err as { code: number }).code === 550) {
throw new NotFoundError();
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/get-uri/src/http.ts
Expand Up @@ -100,13 +100,13 @@ export const http: GetUriProtocol<HttpOptions> = async (url, opts = {}) => {
debug('got %o response status code', code);

// any 2xx response is a "success" code
let type = (code / 100) | 0;
const type = (code / 100) | 0;

// check for a 3xx "redirect" status code
let location = res.headers.location;
const location = res.headers.location;
if (type === 3 && location) {
if (!opts.redirects) opts.redirects = [];
let redirects = opts.redirects;
const redirects = opts.redirects;

if (redirects.length < maxRedirects) {
debug('got a "redirect" status code with Location: %o', location);
Expand All @@ -117,10 +117,10 @@ export const http: GetUriProtocol<HttpOptions> = async (url, opts = {}) => {
// hang on to this Response object for the "redirects" Array
redirects.push(res);

let newUri = new URL(location, url.href);
const newUri = new URL(location, url.href);
debug('resolved redirect URL: %o', newUri.href);

let left = maxRedirects - redirects.length;
const left = maxRedirects - redirects.length;
debug('%o more redirects allowed after this one', left);

// check if redirecting to a different protocol
Expand Down
3 changes: 2 additions & 1 deletion packages/get-uri/src/index.ts
Expand Up @@ -10,6 +10,7 @@ import { https } from './https';

const debug = createDebug('get-uri');

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;

export type GetUriProtocol<T> = (parsed: URL, opts?: T) => Promise<Readable>;
Expand Down Expand Up @@ -72,5 +73,5 @@ export async function getUri<Uri extends string>(
}

const getter = protocols[protocol];
return getter(url, opts as any);
return getter(url, opts as never);
}
16 changes: 8 additions & 8 deletions packages/get-uri/test/ftp.test.ts
Expand Up @@ -11,10 +11,10 @@ describe('get-uri', () => {

beforeAll(async () => {
server = new FtpServer('127.0.0.1', {
// @ts-ignore
// @ts-expect-error ...
logLevel: -1,
getInitialCwd(_socket, fn) {
// @ts-expect-error
// @ts-expect-error ...
fn?.(undefined, '/');
},
getRoot() {
Expand All @@ -24,21 +24,21 @@ describe('get-uri', () => {

server.on('client:connected', (conn) => {
let username: string;
// @ts-expect-error
conn.on('command:user', (user, success, failure) => {
// @ts-expect-error ...
conn.on('command:user', (user, success) => {
username = user;
success();
});
// @ts-expect-error
conn.on('command:pass', (pass, success, failure) => {
// @ts-expect-error ...
conn.on('command:pass', (pass, success) => {
success(username);
});
});

port = await new Promise<number>((r) =>
// @ts-expect-error
// @ts-expect-error ...
server.listen(0, () => {
// @ts-expect-error
// @ts-expect-error ...
r(server.server.address().port);
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/get-uri/test/http.test.ts
Expand Up @@ -16,7 +16,7 @@ describe('get-uri', () => {
// setup target HTTP server
server = http.createServer(st(__dirname));
await listen(server);
// @ts-expect-error
// @ts-expect-error `port` definitely exists
port = server.address().port;
});

Expand Down

0 comments on commit 7541964

Please sign in to comment.