Skip to content

Instantly share code, notes, and snippets.

View kt3k's full-sized avatar

Yoshiya Hinosawa kt3k

View GitHub Profile
addEventListener("fetch", (e) => {
e.respondWith((async () => {
let result: string;
const { request } = e;
if (request.body == null) {
result = "request body is empty\n";
} else {
result = `request body length = ${(await request.arrayBuffer()).byteLength}\n`;
}
return new Response(result)
import "https://unpkg.com/[email protected]/umd/react.development.js";
import "https://unpkg.com/[email protected]/umd/react-dom.development.js";
import "https://unpkg.com/@testing-library/[email protected]/dist/@testing-library/react.umd.js";
import jsdom from "https://dev.jspm.io/jsdom";
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
const { render } = TestingLibraryReact; // <- Umd testing-library exports this in global
Deno.test("test", async () => {
const { JSDOM } = jsdom;
const doc = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
const dirData: Record<string, Uint8Array> = {};
dirData["/bar.ts"] = [Uint8Array.from(atob("Y29uc29sZS5sb2coImJhciIpOwo="), (c) => c.charCodeAt(0)), "text/typescript"];
dirData["/foo.txt"] = [Uint8Array.from(atob("Zm9vCg=="), (c) => c.charCodeAt(0)), "text/plain"];
addEventListener("fetch", (e) => {
const { pathname } = new URL(e.request.url);
const data = dirData[pathname];
if (data) {
const [bytes, mediaType] = data;
e.respondWith(new Response(bytes, { headers: { "content-type": mediaType } }));
return;
addEventListener("fetch", (e: FetchEvent) => {
const authorization = e.request.headers.get("authorization");
console.log("authorization", authorization);
if (!authorization) {
e.respondWith(new Response("", {
status: 401,
statusText: "Unauthorized",
headers: {
addEventListener("fetch", (e) => {
e.respondWith(new Promise((resolve) => {
setTimeout(() => {
resolve(new Response("hello"));
}, 5000);
}));
});
import { parse } from "https://raw.githubusercontent.com/kt3k/deno_std/fix/yaml-compat-deploy/encoding/yaml.ts";
addEventListener("fetch", (e) => {
e.respondWith(new Response(JSON.stringify(parse(`
foo: bar
baz:
- qux
- quux
`))));
});
addEventListener("fetch", (e) => {
const id = Math.random();
console.log(`id=${id}`);
setInterval(() => {
console.log(`id=${id}`);
}, 10000);
e.respondWith(new Promise(() => {}));
});
/**
* [example][]
*
* [example]: https://example.com
*/
export function exampleFunc(src: string): void {}
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
};
/*global self*/
/*jshint latedef: nofunc*/
/*
Distributed under both the W3C Test Suite License [1] and the W3C
3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
@kt3k
kt3k / repro.ts
Last active April 28, 2021 05:01
dts -> ts -> dts
addEventListener("fetch", (e) => {
const pathname = new URL(e.request.url).pathname;
switch (pathname) {
case "/foo.ts": {
e.respondWith(new Response("export * from './bar'", {
headers: {
"content-type": "application/typescript",
}
}));
return;