Skip to content

Instantly share code, notes, and snippets.

View petamoriken's full-sized avatar
🌐
Proceeding with the Web Standards

Kenta Moriuchi petamoriken

🌐
Proceeding with the Web Standards
View GitHub Profile
// Fetch Standard
// prototype pollution
Object.prototype.toJSON = () => ({ foo: "foo" });
const response = Response.json({ bar: "bar" });
console.log(await response.json());
// HTML Standard
const { map, arr, obj } = structuredClone((() => {
const [map, arr, obj] = [new Map(), [], {}];
map.a = "a"; arr.b = "b"; obj.c = "c";
return { map, arr, obj };
})());
console.log(`${map.a} / ${arr.b} / ${obj.c}`);
import idl from "@webref/idl";
const serializables = [];
const transferables = [];
const all = await idl.parseAll();
for (const file of Object.values(all)) {
for (const obj of file) {
if (obj.type !== "interface") {
continue;
@petamoriken
petamoriken / checkTypedArrayTypes.js
Last active August 23, 2021 01:03
Strict TypedArray Type Check inspired by util.types implementation of Node.js
/**
* @file Strict TypedArray Type Check inspired by Node.js util.types implementation
* @see https://github.com/nodejs/node/blob/v16.x/lib/internal/util/types.js
*/
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype;
const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get;
/**
* @param {unknown} value
// TypedArray constructor
// Array
const uint8_1 = new Uint8Array([1, 2, 3]);
console.log(uint8_1);
// => Uint8Array(3) [1, 2, 3]
// ArrayLike
const uint8_2 = new Uint8Array({ 0: 1, 1: 2, 2: 3, length: 3 });
console.log(uint8_2);
let isSupportedAbortSignalOption = false;
try {
const options = {
get signal() {
isSupportedAbortSignalOption = true;
return;
},
};
(window.addEventListener as any)("test", null, options);
import { useEffect, useState } from "react";
/**
* @example
* const matches = useMatchMedia("screen and (min-width: 560px)");
*
* @param raw
*/
export function useMatchMedia(raw: string): boolean {
const [matches, setMatches] = useState(false);
@petamoriken
petamoriken / useResponsive.ts
Last active October 11, 2022 03:41
雑に Responsive Design 用の React Custom Hooks を作ったやつ。
import { useEffect, useMemo, useState } from "react";
/**
* 後続の Iterator Value と合わせた Iterator を返す
* (1, 2, 3) -> ([1, 2], [2, 3], [3, undefined])
*
* @param iterable
*/
function* withNext<T>(iterable: Iterable<T>): IterableIterator<[T, T | undefined]> {
let isStart = true;
@petamoriken
petamoriken / h2o.conf.yaml
Last active February 25, 2022 06:52
partial of h2o config files for mastodon
http2-casper: ON
compress: ON
header.setifempty: "X-XSS-Protection: 1; mode=block"
header.setifempty: "Expect-CT: max-age=2592000, enforce"
header.setifempty: "Expect-Staple: max-age=31536000; includeSubDomains; preload"
listen:
port: 80
@petamoriken
petamoriken / EventTarget.js
Last active March 8, 2020 14:08
fallback EventTarget constructor for Safari <= 13 and IE
const Original = self.EventTarget;
let availabilityOfEventTargetConstructor = true;
try {
new Original();
} catch {
availabilityOfEventTargetConstructor = false;
}
/** @type {{ new(): EventTarget, prototype: EventTarget }} */