moddable-polyfill

polyfill libraries for Moddable SDK

MIT License

Stars
7

moddable-polyfill

polyfill libraries for Moddable SDK

manifest.json to use polyfills

{
  "include": [
      "path/to/manifest_polyfill_esp.json",
  ],
}

example.js

import {} from 'polyfill';

net

WHATWG URL

Note: If you build it for esp8266, update Moddable SDK to the latest

manifest.json to use URL

{
  "include": [
  ],
  "modules": {
    "*": [
      "path/to/URL",
    ]
  },
}

example.js

import {URL} from 'URL';

const url = new URL('https://USER:[email protected]:9090/path/to/doc?se=keyword#hash');
trace(url.href);     // 'https://USER:[email protected]:9090/path/to/doc?se=keyword#hash'
trace(url.protocol); // 'https:'
trace(url.username); // 'USER'
trace(url.password); // 'PASS'
trace(url.origin);   // 'https://example.com:9090'
trace(url.host);     // 'example.com:9090'
trace(url.hostname); // 'example.com'
trace(url.port);     // '9090'
trace(url.pathname); // '/path/to/doc'
trace(url.search);   // '?se=keyword'
trace(url.hash);     // '#hash'

Fetch API

manifest.json to use fetch

{
  "include": [
    "$(MODDABLE)/examples/manifest_base.json",
    "$(MODDABLE)/examples/manifest_net.json",
    "$(MODDABLE)/modules/crypt/tls.json"
  ],
  "modules": {
    "*": [
      "path/to/fetch",
      "path/to/URL",
      "$(MODULES)/network/http/*"
    ]
  },
  "resources":{
    "*": [
      "$(MODULES)/crypt/data/ca109",
      "$(MODULES)/crypt/data/ca106",
    ]
  },
  "preload": [
    "http"
  ],
}

example.js

import {} from 'fetch'; // import as global module.

fetch('https://example.com')
.then(r => r.json())   // if you want to get response as text, call r.text()
.then(j => trace(j))   // JSON
.catch(err => trace(err));

Note: You need to set SNTP server in order to set current time for date validation, if you want to access HTTPS site.

mcconfig -d -m -p esp sntp=pool.ntp.org ssid=foo password=bar

Timer

setInterval

manifest.json to use setInterval

{
  "include": [
  ],
  "modules": {
    "*": [
      "path/to/setInterval",
    ]
  },
}

example.js

import {} from 'setInterval';
import {} from 'clearInterval';

let i = 0;
const id = setInterval(() => {
  trace(`${i}\n`);
  i++;
  if (i === 10) {
    clearTimeout(id);
  }
}, 1000);

setTimeout, setImmediate, clearTimeout and clearImmediate also are same as *Interval.

And all functions can be imported by following code.

import {} from 'w3c_timer';

Console

Note: This is only for esp32/8266. It can't be built for Windows/macOS/Linux.

TBD