This PR finishes adding types (partial types were added in #1592, #1593 did some prerequisite rearrangement).

To generate the types correctly in `pyproxy.js`, I changed the protocols like `PyProxyLengthMethods` into ES6 classes (they used to just be objects). This was needed because otherwise Typescript makes the protocol into a namespace but I want it to be a type to indicate that an object has certain methods. To compensate for this change, I had to modify `getPyProxyClass` accordingly. This change required a number of syntactical changes.

I also added a bunch of predicate methods to the base `PyProxy` class to test whether the proxy supports certain extra methods. These are needed to use as Typescript type guards, but they probably are also useful by themselves when code needs to do reflection on the proxies.

I don't know whether the exposed types or the type guard predicates are well named, I would be happy for recommendations of improvements to the names -- both the type names and the predicates are public and deserve to be named with care.

JsDoc does not recognize typescript type guards or typescript type definitions, so I added a JsDoc plugin which strips out type definitions and transforms typeguards `x is y` ==> `boolean`. There's one JsDoc typescript parse error that still happens but it doesn't cause any harm to the output.

I also changed the way that we collect javascript API docs to be a bit more robust. In particular, it used to be organized by file. I changed it as follows: 
1. any API part of `globalThis` or marked with `@memberof globalThis` is treated as a global, 
2. any API of a class whose name starts with `PyProxy` is treated as a part of the `PyProxy` API,
3. anything else is assumed to be part of the pyodide API

As before, things marked `@private` are left out.

I also added tests for the type declarations using `tpd`. They are a new CI step.


ENH More complete typescript types. This PR finishes adding types (partial types were added in #1592, #1593 did some prerequisite rearrangement).

To generate the types correctly in `pyproxy.js`, I changed the protocols like `PyProxyLengthMethods` into ES6 classes (they used to just be objects). This was needed because otherwise Typescript makes the protocol into a namespace but I want it to be a type to indicate that an object has certain methods. To compensate for this change, I had to modify `getPyProxyClass` accordingly. This change required a number of syntactical changes.

I also added a bunch of predicate methods to the base `PyProxy` class to test whether the proxy supports certain extra methods. These are needed to use as Typescript type guards, but they probably are also useful by themselves when code needs to do reflection on the proxies.

I don't know whether the exposed types or the type guard predicates are well named, I would be happy for recommendations of improvements to the names -- both the type names and the predicates are public and deserve to be named with care.

JsDoc does not recognize typescript type guards or typescript type definitions, so I added a JsDoc plugin which strips out type definitions and transforms typeguards `x is y` ==> `boolean`. There's one JsDoc typescript parse error that still happens but it doesn't cause any harm to the output.

I also changed the way that we collect javascript API docs to be a bit more robust. In particular, it used to be organized by file. I changed it as follows: 
1. any API part of `globalThis` or marked with `@memberof globalThis` is treated as a global, 
2. any API of a class whose name starts with `PyProxy` is treated as a part of the `PyProxy` API,
3. anything else is assumed to be part of the pyodide API

As before, things marked `@private` are left out.

I also added tests for the type declarations using `tpd`. They are a new CI step.


version: 2

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: pyodide/pyodide-env:16
  environment:
    - EMSDK_NUM_CORES: 3
      EMCC_CORES: 3
      PYODIDE_JOBS: 3

jobs:
  lint:
    <<: *defaults
    resource_class: small
    steps:
      - checkout

      - run:
          name: lint
          command: make lint

  test-docs:
    <<: *defaults
    resource_class: small
    steps:
      - checkout

      - run:
          name: Install prerequisites
          command: |
            pip install -r docs/requirements-doc.txt

      - run:
          name: Test docs
          command: pytest docs/sphinx_pyodide/tests

  build-core:
    <<: *defaults
    steps:
      - checkout

      - restore_cache:
          keys:
          - -{{ checksum "Makefile.envs" }}-v20201205-

      - run:
          name: build emsdk
          no_output_timeout: 1200
          command: |
            ccache -z
            make -C emsdk
            ccache -s

      - run:
          name: build cpython
          no_output_timeout: 1200
          command: |
            ccache -z
            make -C cpython
            ccache -s

      - run:
          name: build pyodide core
          no_output_timeout: 1200
          command: |
            ccache -z
            # The following packages are currently used in the main pyodide test suite
            PYODIDE_PACKAGES="micropip,pyparsing,pytz,packaging,kiwisolver,Jinja2" make
            ccache -s

      - run:
          name: check-size
          command: ls -lh build/

      - save_cache:
          paths:
            - ~/.ccache
          key: -{{ checksum "Makefile.envs" }}-v20201205-{{ .BuildNum }}

      - run:
          name: Clean up workspace
          command: |
            rm -rf cpython/{build,downloads}
            rm -rf emsdk/emsdk/binaryen

      - persist_to_workspace:
          root: .
          paths:
            - .


      - store_artifacts:
          path: /root/repo/build/

  build-packages:
    <<: *defaults
    steps:
      - checkout

      - attach_workspace:
          at: .

      - restore_cache:
          keys:
          - -{{ checksum "Makefile.envs" }}-v20201205-

      - run:
          name: build packages
          no_output_timeout: 1800
          command: |
            ccache -z
            make
            ccache -s

      - run:
          name: check-size
          command: ls -lh build/

      - save_cache:
          paths:
            - ~/.ccache
          key: -{{ checksum "Makefile.envs" }}-v20201205-{{ .BuildNum }}

      - persist_to_workspace:
          root: .
          paths:
            - ./packages/.artifacts
            - ./build

      - store_artifacts:
          path: /root/repo/build/

  test-core-firefox:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py src packages/micropip/ -v -k firefox -n 3

  test-core-chrome:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py src packages/micropip/ -v -k chrome

  test-packages-firefox:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py packages/test* packages/*/test* -v -k firefox

  test-packages-chrome:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py packages/test* packages/*/test* -v -k chrome -n 2

  test-emsdk:
    <<: *defaults
    resource_class: small
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: make -C emsdk test

  test-python:
    <<: *defaults
    resource_class: small
    steps:
      - checkout
      - run:
          name: test
          command: |
            pytest src pyodide-build packages/micropip/ -v -k 'not (chrome or firefox)' --cov=pyodide_build --cov=pyodide

  benchmark:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: benchmark
          command: |
            python benchmark/benchmark.py /usr/local/bin/python3 build/benchmarks.json
      - store_artifacts:
          path: /root/repo/build/benchmarks.json

  deploy-release:
    # To reduce chance of deployment issues, try to keep the steps here as
    # similar as possible to the steps in deploy-dev!
    resource_class: small

    docker:
      - image: cibuilds/github:0.13

    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: Install requirements
          command: |
            apk add --no-cache --update python3 make
            python3 -m pip install awscli
      - run:
          name: Deploy Github Releases
          command: |
            cp -r build /tmp/pyodide
            cd /tmp/
            tar cjf pyodide-build-${CIRCLE_TAG}.tar.bz2  pyodide/
            ghr -t "${GITHUB_TOKEN}" -u "${CIRCLE_PROJECT_USERNAME}" -r "${CIRCLE_PROJECT_REPONAME}" -c "${CIRCLE_SHA1}" -delete  "${CIRCLE_TAG}" ./pyodide-build-${CIRCLE_TAG}.tar.bz2

      - run:
          name: Set PYODIDE_BASE_URL
          command: PYODIDE_BASE_URL="https://cdn.jsdelivr.net/pyodide/v${CIRCLE_TAG}/full/" make update_base_url
      - run:
          name: Deploy to pyodide-cdn2.iodide.io
          command: |
            find build/ -type f -exec sh -c "echo \"Compressing {}\"; gzip {}; mv {}.gz {};" \;
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/v${CIRCLE_TAG}/full/" --exclude '*.data' --cache-control 'max-age=30758400, immutable, public' --content-encoding 'gzip'    # 1 year cache
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/v${CIRCLE_TAG}/full/" --exclude '*' --include '*.data' --cache-control 'max-age=30758400, immutable, public'  --content-type 'application/wasm' --content-encoding 'gzip'  # 1 year
            # update 301 redirect for the /latest/* route.
            aws s3api put-bucket-website --cli-input-json file://.circleci/s3-website-config.json

  deploy-dev:
    # To reduce chance of deployment issues, try to keep the steps here as
    # similar as possible to the steps in deploy-release!
    resource_class: small

    docker:
      - image: cibuilds/github:0.13

    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: Install requirements
          command: |
            apk add --no-cache --update python3 make
            python3 -m pip install awscli
      - run:
          name: Set PYODIDE_BASE_URL
          command: PYODIDE_BASE_URL="https://cdn.jsdelivr.net/pyodide/dev/full/" make update_base_url
      - run:
          name: Deploy to pyodide-cdn2.iodide.io
          command: |
            find build/ -type f -exec sh -c "echo \"Compressing {}\"; gzip {}; mv {}.gz {};" \;
            aws s3 rm --recursive "s3://pyodide-cdn2.iodide.io/dev/full/"
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/dev/full/" --exclude '*.data' --cache-control 'max-age=3600, public' --content-encoding 'gzip'  # 1 hour cache
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/dev/full/" --exclude '*' --include '*.data' --cache-control 'max-age=3600, public'  --content-type 'application/wasm' --content-encoding 'gzip'  # 1 hour cache

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - lint
      - test-docs
      - build-core:
          filters:
            tags:
              only: /.*/
      - build-packages:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-core-chrome:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-core-firefox:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-packages-chrome:
          requires:
            - build-packages
          filters:
            tags:
              only: /.*/
      - test-packages-firefox:
          requires:
            - build-packages
          filters:
            tags:
              only: /.*/
      - test-emsdk:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-python:
          filters:
            tags:
              only: /.*/
      - benchmark:
          requires:
            - build-packages
      - deploy-release:
          requires:
            - lint
            - test-python
            - test-core-firefox
            - test-packages-firefox
            - test-emsdk
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^\d+\.\d+\.\w+$/
      - deploy-dev:
          requires:
            - lint
            - test-python
            - test-core-firefox
            - test-packages-firefox
            - test-emsdk
          filters:
            branches:
              only: main

version: 2

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: pyodide/pyodide-env:16
  environment:
    - EMSDK_NUM_CORES: 3
      EMCC_CORES: 3
      PYODIDE_JOBS: 3

jobs:
  lint:
    <<: *defaults
    resource_class: small
    steps:
      - checkout

      - run:
          name: lint
          command: make lint

  test-docs:
    <<: *defaults
    resource_class: small
    steps:
      - checkout

      - run:
          name: Install prerequisites
          command: |
            pip install -r docs/requirements-doc.txt

      - run:
          name: Test docs
          command: pytest docs/sphinx_pyodide/tests

  build-core:
    <<: *defaults
    steps:
      - checkout

      - restore_cache:
          keys:
          - -{{ checksum "Makefile.envs" }}-v20201205-

      - run:
          name: build emsdk
          no_output_timeout: 1200
          command: |
            ccache -z
            make -C emsdk
            ccache -s

      - run:
          name: build cpython
          no_output_timeout: 1200
          command: |
            ccache -z
            make -C cpython
            ccache -s

      - run:
          name: build pyodide core
          no_output_timeout: 1200
          command: |
            ccache -z
            # The following packages are currently used in the main pyodide test suite
            PYODIDE_PACKAGES="micropip,pyparsing,pytz,packaging,kiwisolver,Jinja2" make
            ccache -s

      - run:
          name: check-size
          command: ls -lh build/

      - save_cache:
          paths:
            - ~/.ccache
          key: -{{ checksum "Makefile.envs" }}-v20201205-{{ .BuildNum }}

      - run:
          name: Clean up workspace
          command: |
            rm -rf cpython/{build,downloads}
            rm -rf emsdk/emsdk/binaryen

      - persist_to_workspace:
          root: .
          paths:
            - .


      - store_artifacts:
          path: /root/repo/build/

  build-packages:
    <<: *defaults
    steps:
      - checkout

      - attach_workspace:
          at: .

      - restore_cache:
          keys:
          - -{{ checksum "Makefile.envs" }}-v20201205-

      - run:
          name: build packages
          no_output_timeout: 1800
          command: |
            ccache -z
            make
            ccache -s

      - run:
          name: check-size
          command: ls -lh build/

      - save_cache:
          paths:
            - ~/.ccache
          key: -{{ checksum "Makefile.envs" }}-v20201205-{{ .BuildNum }}

      - persist_to_workspace:
          root: .
          paths:
            - ./packages/.artifacts
            - ./build

      - store_artifacts:
          path: /root/repo/build/

  test-core-firefox:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py src packages/micropip/ -v -k firefox -n 3

  test-core-chrome:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py src packages/micropip/ -v -k chrome

  test-packages-firefox:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py packages/test* packages/*/test* -v -k firefox

  test-packages-chrome:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            tools/pytest_wrapper.py packages/test* packages/*/test* -v -k chrome -n 2

  test-emsdk:
    <<: *defaults
    resource_class: small
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: make -C emsdk test

  test-python:
    <<: *defaults
    resource_class: small
    steps:
      - checkout
      - run:
          name: test
          command: |
            pytest src pyodide-build packages/micropip/ -v -k 'not (chrome or firefox)' --cov=pyodide_build --cov=pyodide

  test-js-types:
    <<: *defaults
    resource_class: small
    steps:
      - attach_workspace:
          at: .
      - run:
          name: test
          command: |
            cd src/js
            npx tsd


  benchmark:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: benchmark
          command: |
            python benchmark/benchmark.py /usr/local/bin/python3 build/benchmarks.json
      - store_artifacts:
          path: /root/repo/build/benchmarks.json

  deploy-release:
    # To reduce chance of deployment issues, try to keep the steps here as
    # similar as possible to the steps in deploy-dev!
    resource_class: small

    docker:
      - image: cibuilds/github:0.13

    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: Install requirements
          command: |
            apk add --no-cache --update python3 make
            python3 -m pip install awscli
      - run:
          name: Deploy Github Releases
          command: |
            cp -r build /tmp/pyodide
            cd /tmp/
            tar cjf pyodide-build-${CIRCLE_TAG}.tar.bz2  pyodide/
            ghr -t "${GITHUB_TOKEN}" -u "${CIRCLE_PROJECT_USERNAME}" -r "${CIRCLE_PROJECT_REPONAME}" -c "${CIRCLE_SHA1}" -delete  "${CIRCLE_TAG}" ./pyodide-build-${CIRCLE_TAG}.tar.bz2

      - run:
          name: Set PYODIDE_BASE_URL
          command: PYODIDE_BASE_URL="https://cdn.jsdelivr.net/pyodide/v${CIRCLE_TAG}/full/" make update_base_url
      - run:
          name: Deploy to pyodide-cdn2.iodide.io
          command: |
            find build/ -type f -exec sh -c "echo \"Compressing {}\"; gzip {}; mv {}.gz {};" \;
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/v${CIRCLE_TAG}/full/" --exclude '*.data' --cache-control 'max-age=30758400, immutable, public' --content-encoding 'gzip'    # 1 year cache
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/v${CIRCLE_TAG}/full/" --exclude '*' --include '*.data' --cache-control 'max-age=30758400, immutable, public'  --content-type 'application/wasm' --content-encoding 'gzip'  # 1 year
            # update 301 redirect for the /latest/* route.
            aws s3api put-bucket-website --cli-input-json file://.circleci/s3-website-config.json

  deploy-dev:
    # To reduce chance of deployment issues, try to keep the steps here as
    # similar as possible to the steps in deploy-release!
    resource_class: small

    docker:
      - image: cibuilds/github:0.13

    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: Install requirements
          command: |
            apk add --no-cache --update python3 make
            python3 -m pip install awscli
      - run:
          name: Set PYODIDE_BASE_URL
          command: PYODIDE_BASE_URL="https://cdn.jsdelivr.net/pyodide/dev/full/" make update_base_url
      - run:
          name: Deploy to pyodide-cdn2.iodide.io
          command: |
            find build/ -type f -exec sh -c "echo \"Compressing {}\"; gzip {}; mv {}.gz {};" \;
            aws s3 rm --recursive "s3://pyodide-cdn2.iodide.io/dev/full/"
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/dev/full/" --exclude '*.data' --cache-control 'max-age=3600, public' --content-encoding 'gzip'  # 1 hour cache
            aws s3 sync build/ "s3://pyodide-cdn2.iodide.io/dev/full/" --exclude '*' --include '*.data' --cache-control 'max-age=3600, public'  --content-type 'application/wasm' --content-encoding 'gzip'  # 1 hour cache

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - lint
      - test-docs
      - build-core:
          filters:
            tags:
              only: /.*/
      - build-packages:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-core-chrome:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-core-firefox:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-packages-chrome:
          requires:
            - build-packages
          filters:
            tags:
              only: /.*/
      - test-packages-firefox:
          requires:
            - build-packages
          filters:
            tags:
              only: /.*/
      - test-emsdk:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - test-python:
          filters:
            tags:
              only: /.*/
      - test-js-types:
          requires:
            - build-core
          filters:
            tags:
              only: /.*/
      - benchmark:
          requires:
            - build-packages
      - deploy-release:
          requires:
            - lint
            - test-python
            - test-core-firefox
            - test-packages-firefox
            - test-emsdk
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^\d+\.\d+\.\w+$/
      - deploy-dev:
          requires:
            - lint
            - test-python
            - test-core-firefox
            - test-packages-firefox
            - test-emsdk
          filters:
            branches:
              only: main
