# frame-worker

> Run Worker Script inside an Iframe

Latest version **0.3.0** (published 2023-08-06) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install frame-worker
pnpm add frame-worker
yarn add frame-worker
bun add frame-worker
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2023-08-06 |
| First published | 2022-11-06 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Daniel J. Dufour |
| Maintainers | danieljdufour |
| Keywords | frame, iframe, web, worker |

## Links

- npm: https://www.npmjs.com/package/frame-worker
- Repository: https://github.com/DanielJDufour/frame-worker
- Homepage: https://github.com/DanielJDufour/frame-worker#readme
- Issues: https://github.com/DanielJDufour/frame-worker/issues
- npm.io page: https://npm.io/package/frame-worker

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2023-08-06
- 0.2.0 — 2023-01-23
- 0.1.0 — 2022-11-06

## README

# Frame-Worker
> Run Web Worker Script with an IFrame

## features
- default export detection
- url shortening
- blob url revocation
- raw unencoded strings
- CommonJS code

## install
### via script tag
```html
<script src="https://unpkg.com/frame-worker"></script>
```
### via npm
```sh
npm install frame-worker
```

## basic usage
```js
import FrameWorker from "frame-worker";

// initialize from url
new FrameWorker("./square.worker.js");

// intialize from blob url
new FrameWorker("blob:http://localhost:8080/bf331630-1275-4b3d-9b65-22512egdb592");

// initialize from data url
new FrameWorker("data:application/javascript;base64,Cm1vZHVsZS5leHBvcnRzLmRlZmF1bHQgPSBmdW5jdGlvbigpIHsKICByZXR1cm4gMTIzNDsKfQ==");

// listen to messages
worker.addEventListener("message", function (event) {
  console.log("received": event.data);
});
```

## advanced usage
### context
You can pass an extra context object to the worker. FrameWorker will assign the entries in the object
to the global `window` and `self` inside the iframe.  In the example below, we pass a validate function
to the FrameWorker.
```js
new FrameWorker(workerScript, undefined, { 
  context: {
    validate: function() { ... }
  }
});
```

### cjs
You can pass in a CommonJS module and have it automatically interpreted as a Web Worker script.
If there is no self.onmessage defined in the input script, FrameWorker will automatically look for and call `module.exports` or `module.exports.default` function.
```js
const code = `
  module.exports = function() {
    return 1234;
  }
`;
new FrameWorker(code, undefined, {
  cjs: true, // code is a basic CommonJS file
  raw: true // code is not encoded in a Data URL or Blob URL
});
```

### debug
You can set debug to true for extra helpful log messages
```js
new FrameWorker(workerScript, undefined, { debug: true });
```

### id
You can specify an id to give to the created iframe.
If id is not a string, it will be ignored.
```js
new FrameWorker(workerScript, undefined, { id: "special" });
```

### raw
You can pass in raw unencoded JavaScript.
```js
new FrameWorker("console.log('hello, world')", undefined, { raw: true });
```

### revoke
If you initialize a FrameWorker with a `"blob:..."` url,
you can have the url automatically revoked after the worker is initialized.
```js
const url = "blob:http://localhost:8080/bf331630-1275-4b3d-9b65-22512egdb592";
new FrameWorker(url, undefined, { revoke: true });
```

---
_Source: https://npm.io/package/frame-worker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
