Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Aug 3, 2024
1 parent dae5b9e commit 4d0a706
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
node_modules/
17 changes: 17 additions & 0 deletions types/console.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export {};

declare global {
/**
* Console interface for logging.
*
* Currently logs are only available in the backend logs.
* See https://docs.caido.io/report_bug.html#1-backend-logs
*/
type Console = {
debug(message: any): void;
log(message: any): void;
warn(message: any): void;
error(message: any): void;
};
var console: Console;
}
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// <reference path="console.d.ts" />
/// <reference path="timers.d.ts" />
/// <reference path="url.d.ts" />
26 changes: 26 additions & 0 deletions types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "rquickjs-extra-types",
"license": "Apache-2.0",
"version": "0.0.1",
"main": "",
"types": "index.d.ts",
"description": "Type definitions for rquickjs-extra",
"repository": {
"type": "git",
"url": "https://github.com/rquickjs/rquickjs-extra",
"directory": "types"
},
"files": [
"*.d.ts"
],
"devDependencies": {
"typescript": "^5.5.3"
},
"scripts": {
"test": "tsc"
},
"keywords": [
"quickjs",
"types"
]
}
24 changes: 24 additions & 0 deletions types/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions types/timers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export {};

declare global {
/**
* This object is created internally and is returned from `setTimeout()` and `setInterval()`. It can be passed to either `clearTimeout()` or `clearInterval()` in order to cancel the
* scheduled actions.
*/
class Timeout {}

/**
* Schedules execution of a one-time `callback` after `delay` milliseconds.
*
* The `callback` will likely not be invoked in precisely `delay` milliseconds.
* Caido makes no guarantees about the exact timing of when callbacks will fire,
* nor of their ordering. The callback will be called as close as possible to the
* time specified.
*
* When `delay` is less than `4`, the `delay` will be set to `4`.
*
* @param callback The function to call when the timer elapses.
* @param [delay=4] The number of milliseconds to wait before calling the `callback`.
* @return for use with {@link clearTimeout}
*/
function setTimeout<TArgs extends any[]>(
callback: (...args: TArgs) => void,
ms?: number,
): Timeout;

/**
* Cancels a `Timeout` object created by `setTimeout()`.
* @param timeout A `Timeout` object as returned by {@link setTimeout}.
*/
function clearTimeout(timeout: Timeout): void;

/**
* Schedules repeated execution of `callback` every `delay` milliseconds.
*
* When `delay` isless than `4`, the `delay` will be set to `4`.
*
* @param callback The function to call when the timer elapses.
* @param [delay=4] The number of milliseconds to wait before calling the `callback`.
* @return for use with {@link clearInterval}
*/
function setInterval<TArgs extends any[]>(
callback: (...args: TArgs) => void,
ms?: number,
): Timeout;

/**
* Cancels a `Timeout` object created by `setInterval()`.
* @param timeout A `Timeout` object as returned by {@link setInterval}
*/
function clearInterval(interval: Timeout): void;

/**
* Schedules the "immediate" execution of the `callback` after I/O events'
* callbacks.
*
* @param callback The function to call at the end of this turn of the Node.js `Event Loop`
* @return for use with {@link clearImmediate}
*/
function setImmediate<TArgs extends any[]>(
callback: (...args: TArgs) => void,
): void;
}
17 changes: 17 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"skipLibCheck": false,
"strict": true,
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"disableSolutionSearching": true,
"noUnusedLocals": true,
"noEmit": true,
"resolveJsonModule": true,
"types": []
},
"exclude": ["dist", "node_modules"]
}
134 changes: 134 additions & 0 deletions types/url.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
export {};

declare global {
/**
* The URLSearchParams interface defines utility methods to work with the query string of a URL.
*/
class URLSearchParams implements Iterable<[string, string]> {
constructor(
init?:
| URLSearchParams
| string
| { readonly [name: string]: string }
| Iterable<readonly [name: string, value: string]>
| ReadonlyArray<readonly [name: string, value: string]>,
);
/**
* Append a new name-value pair to the query string.
*/
append(name: string, value: string): void;
/**
* If `value` is provided, removes all name-value pairs
* where name is `name` and value is `value`.
*
* If `value` is not provided, removes all name-value pairs whose name is `name`.
*/
delete(name: string, value?: string): void;
/**
* Returns an ES6 `Iterator` over each of the name-value pairs in the query.
* Each item of the iterator is a JavaScript `Array`. The first item of the `Array` is the `name`, the second item of the `Array` is the `value`.
*
* Alias for `urlSearchParams[@@iterator]()`.
*/
entries(): IterableIterator<[string, string]>;
/**
* Iterates over each name-value pair in the query and invokes the given function.
*
* ```js
* const myURL = new URL('https://example.org/?a=b&#x26;c=d');
* myURL.searchParams.forEach((value, name) => {
* console.log(name, value);
* });
* // Prints:
* // a b
* // c d
* ```
* @param fn Invoked for each name-value pair in the query
*/
forEach(fn: (value: string, name: string) => void): void;
/**
* Returns the value of the first name-value pair whose name is `name`. If there
* are no such pairs, `null` is returned.
* @return or `null` if there is no name-value pair with the given `name`.
*/
get(name: string): string | null;
/**
* Returns the values of all name-value pairs whose name is `name`. If there are
* no such pairs, an empty array is returned.
*/
getAll(name: string): string[];
/**
* Checks if the `URLSearchParams` object contains key-value pair(s) based on `name` and an optional `value` argument.
*
* If `value` is provided, returns `true` when name-value pair with
* same `name` and `value` exists.
*
* If `value` is not provided, returns `true` if there is at least one name-value
* pair whose name is `name`.
*/
has(name: string, value?: string): boolean;
/**
* Returns an ES6 `Iterator` over the names of each name-value pair.
*
* ```js
* const params = new URLSearchParams('foo=bar&#x26;foo=baz');
* for (const name of params.keys()) {
* console.log(name);
* }
* // Prints:
* // foo
* // foo
* ```
*/
keys(): IterableIterator<string>;
/**
* Sets the value in the `URLSearchParams` object associated with `name` to `value`. If there are any pre-existing name-value pairs whose names are `name`,
* set the first such pair's value to `value` and remove all others. If not,
* append the name-value pair to the query string.
*
* ```js
* const params = new URLSearchParams();
* params.append('foo', 'bar');
* params.append('foo', 'baz');
* params.append('abc', 'def');
* console.log(params.toString());
* // Prints foo=bar&#x26;foo=baz&#x26;abc=def
*
* params.set('foo', 'def');
* params.set('xyz', 'opq');
* console.log(params.toString());
* // Prints foo=def&#x26;abc=def&#x26;xyz=opq
* ```
*/
set(name: string, value: string): void;
/**
* The total number of parameter entries.
*/
readonly size: number;
/**
* Sort all existing name-value pairs in-place by their names. Sorting is done
* with a [stable sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability), so relative order between name-value pairs
* with the same name is preserved.
*
* This method can be used, in particular, to increase cache hits.
*
* ```js
* const params = new URLSearchParams('query[]=abc&#x26;type=search&#x26;query[]=123');
* params.sort();
* console.log(params.toString());
* // Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search
* ```
*/
sort(): void;
/**
* Returns the search parameters serialized as a string, with characters
* percent-encoded where necessary.
*/
toString(): string;
/**
* Returns an ES6 `Iterator` over the values of each name-value pair.
*/
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}
}

0 comments on commit 4d0a706

Please sign in to comment.