Skip to content

Wrapped global breaks JS running in service worker. #28

Description

@Pantura

https://github.com/mbullington/node_preamble.dart/blob/master/lib/preamble.dart#L9-L11

Is there a reason why the global/window is wrapped in an object? 327ac2e introduced the change but I do not get the reason for encapsulation. Couldn't find an issue this was related to.

Webworker context won't allow calling for example self.location once going through Object.create. Edit: not only Webworker, you can paste Object.create(window).location to browser console to get the illegal invocation error as well.

I suggest with help of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#search_for_the_global_across_environments
to change this to (globalThis would wrap the first function but can that be support requirement?)

var getGlobal = function () {
  // webworker
  if (typeof self !== 'undefined') { return self; }
  // browser
  if (typeof window !== 'undefined') { return window; }
  // node
  if (typeof global !== 'undefined') { return global; }
  throw new Error('unable to locate global object');
};

var self = getGlobal();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions