Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums only exported as types #5630

Closed
5 tasks done
ericmatthys opened this issue Jul 5, 2023 · 16 comments · Fixed by #5930
Closed
5 tasks done

Enums only exported as types #5630

ericmatthys opened this issue Jul 5, 2023 · 16 comments · Fixed by #5930
Milestone

Comments

@ericmatthys
Copy link

What version of Hls.js are you using?

1.4.7

What browser (including version) are you using?

Chrome 115

What OS (including version) are you using?

macOS Ventura

Test stream

N/A

Configuration

{}

Additional player setup steps

No response

Checklist

Steps to reproduce

  1. Import the Events enum like import HlsJs, { Events } from 'hls.js';
  2. Reference this enum in code like player.on(Events.MEDIA_ATTACHED, onMediaAttached);

Expected behaviour

The enum is exported as a value that can be used, not just a type.

What actually happened?

There is no type error, but at runtime, this code will error.

Console output

`Uncaught TypeError: Cannot read properties of undefined (reading 'MEDIA_ATTACHED')`

Chrome media internals output

No response

@ericmatthys ericmatthys added Bug Needs Triage If there is a suspected stream issue, apply this label to triage if it is something we should fix. labels Jul 5, 2023
@robwalch
Copy link
Collaborator

Hi @ericmatthys,

Can you provide more information about your setup (TypeScript version, etc)?

As a workaround, Events is also available on the default Hls class, so in your example, you should be able to use HlsJs.Events.

@ericmatthys
Copy link
Author

Sure, we're using TypeScript 5.1.3 currently and it's being imported into a Next.js 13.2.4 app.

HlsJs.Events works for something like player.on(HlsJs.Events.MEDIA_ATTACHED, onMediaAttached); but the Events enum is still needed for something like HlsListeners[Events.ERROR], so we'd end up using both depending on if we need it as a value or as an index type.

@robwalch
Copy link
Collaborator

robwalch commented Jul 12, 2023

So what you are looking for is a change in hls.ts to export Events but not strictly as a type as we do here?

hls.js/src/hls.ts

Lines 877 to 881 in c39db5b

export type {
MediaPlaylist,
ErrorDetails,
ErrorTypes,
Events,

Would you need the same for ErrorTypes and ErrorDetails? Any other enums whose values are present in event objects that would require comparison? To be safe we could cover all enums listed in https://github.com/video-dev/hls.js/blob/v1.4.8/api-extractor/report/hls.js.api.md

@robwalch robwalch removed Needs Triage If there is a suspected stream issue, apply this label to triage if it is something we should fix. cannot reproduce labels Jul 12, 2023
@robwalch robwalch added this to the 1.5.0 milestone Jul 12, 2023
@ericmatthys
Copy link
Author

I think so. In general I'd assume enums would be exported so that they can be used as values and not purely as types.

@robwalch
Copy link
Collaborator

Thanks @ericmatthys,

Would you be willing to submit a PR?

@robwalch
Copy link
Collaborator

Hi @ericmatthys,

  1. Import the Events enum like import HlsJs, { Events } from 'hls.js';
  2. Reference this enum in code like player.on(Events.MEDIA_ATTACHED, onMediaAttached);

Did you ever try importing types as types separately from the library like this?

import type { Events } from 'hls.js';
import HlsJs from 'hls.js';

This works for me, since hls.js.d.ts is the default for handling types and hls.mjs the default for modules.

@ferferga
Copy link

Reading through the linked PRs, I'm seeing this was slated for 1.5.x but it's now into the 1.6.x milestone.

If possible, I'd like to have this reconsidered, since from what I'm seeing, the major hold up originally was to provide ESM, but that's already on the roadmap for 1.5.x as far I can see? (although maybe I'm missing something, sorry in that case).

Although this is minor inconvenience, the reasons to get this fixed are:

  • Less boilerplate code (more concise too!)
  • No warnings in one of the most popular linter stacks: eslint + eslint-plugin-import (currently this is raising warnings in the import/no-named-as-default-member rule)

@robwalch
Copy link
Collaborator

Hi @ferferga,

It looks like we would need to introduce a breaking change to the ESM output module (hls.mjs) that would replace the default export with a named one. Then we could include other named exports.

@robwalch
Copy link
Collaborator

Interested in picking up from #5685 (review) ?

@ferferga
Copy link

ferferga commented Oct 24, 2023

@robwalch Can 1.5.x live with those breaking changes?

@robwalch
Copy link
Collaborator

robwalch commented Oct 24, 2023

Is anyone using .mjs at runtime? If so, then no, we cannot change the default export without breaking embeds (and not just build/import statements). We'd need a way for rollup to allow a default with named exports to keep backwards compatibility. Your eslint rule will still fail unless we can have Hls in a named and default export.

So is there a way to work around this error in rollup so we can have it both ways?

[!] Error: Entry module "src/hls.ts" is using named and default exports together. Consumers of your bundle will have to use `Hls.default` to access the default export, which may not be what you want. Use `output.exports: "named"` to disable this warning

@robwalch
Copy link
Collaborator

So can anyone take this on?

Provide a backwards compatible build change with only minimal export changes to hls.ts. We can then set a milestone after the solution passes review.

@ferferga
Copy link

@robwalch By a quick walkthrough, it looks to me that the build process is greatly convoluted.

Is a major overhaul of the pipeline undesired? A migration to Vite maybe?

@robwalch
Copy link
Collaborator

robwalch commented Oct 24, 2023

Is a major overhaul of the pipeline undesired? A migration to Vite maybe?

It is undesired. Is it really necessary to resolve this issue as described? What questions do you have about the build pipeline?

Here's the default export:

export default class Hls implements HlsEventEmitter {

Type exports begin here (I don't think we need any change to these):

hls.js/src/hls.ts

Lines 931 to 934 in ef81686

export type {
MediaPlaylist,
ErrorDetails,
ErrorTypes,

build task is rollup:

"build": "rollup --config && npm run build:types",

rollup configs are defined here:

hls.js/build-config.js

Lines 309 to 312 in ef81686

fullEsm: buildRollupConfig({
type: BUILD_TYPE.full,
format: FORMAT.esm,
minified: false,

@ferferga
Copy link

ferferga commented Oct 24, 2023

@robwalch
I don't have any questions, and I think I got quite a good grasp of it by my first dive. Thank you very much for the extra insights anyway!

It's just that everything it's independent from each other, which leads to confusing behaviour, like the one this issue exposes:

  • Because Typescript is just for declaration emitting, there are no warnings in the IDE about types not being exportable (with Rollup failing).

    • They are mismatched, as you can see from this issue: Typescript doesn't warn consumers about the lack of default export, because it consider it is available. But it's not.
  • The variants could be simplified by having 2 entry points, each with their own proper types and import resolution when consuming.

  • Vite has first-class ESM and TypeScript for typed configs and typed build scripts.

  • With npm workspaces, a package for every build type can be published to be directly and explicitly pulled by consumers.

I also recall some of us (I contribute to @jellyfin) also had issues with the builds (issues with ES5 iirc) and contributed PRs, issues I also thought back at the time that were mainly due to a messy building process.

All in all, I see the current building pipeline is a castle glued with toothpaste built upon the beginning of the times, when this project was not in TS, so it's a good chance to maybe start tackling it. But, of course if its undesired, I can try to see if it can be "workarounded".

TLDR: This issue wouldn't be here in the first place if it weren't for the build process.

@robwalch
Copy link
Collaborator

robwalch commented Oct 24, 2023

Thanks @ferferga,

We could take changes to the build process if they were backwards compatible and solved additional needs of our users.

You did however ask if this could be included in v1.5 and the timeframe for that is short - we're almost feature complete and need to focus the beta on testing those features (not the build).

Here's a PR that adds named exports with minimal changes to the build process. Can you verify that it solves this issue for you? I managed to limit the scope to only the ESM output.

Would you be willing to contribute an alternative build process / proof-of-concept that we could review? The next or following milestone (v1.6-1.7) would be appropriate for a build refactor.

@robwalch robwalch added this to the 1.5.0 milestone Oct 25, 2023
@robwalch robwalch added Enhancement and removed Bug labels Oct 25, 2023
robwalch added a commit that referenced this issue Nov 3, 2023
robwalch added a commit that referenced this issue Nov 6, 2023
eowino added a commit to DiceTechnology/hls.js that referenced this issue Jan 15, 2024
* chore(deps): update dependency chromedriver to v118 (video-dev#5919)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update dependency lint-staged to v15 (video-dev#5920)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update tjenkinson/gh-action-auto-merge-dependency-updates action to v1.3.5 (video-dev#5922)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update dependency lint-staged to v15.0.1

* chore(deps): update dependency lint-staged to v15.0.2

* chore(deps): update dependency chromedriver to v118.0.1

* chore(deps): update dependency @rollup/plugin-replace to v5.0.4

* chore(deps): update dependency wrangler to v3.13.2

* chore(deps): update dependency wrangler to v3.14.0

* chore(deps): update dependency @types/chai to v4.3.9

* chore(deps): update dependency @rollup/plugin-commonjs to v25.0.7

* chore(deps): update typescript-eslint monorepo to v6.8.0

* chore(deps): update typescript-eslint monorepo to v6.9.0

* chore(deps): update dependency @types/mocha to v10.0.3

* chore(deps): update dependency @types/chart.js to v2.9.39

* chore(deps): update dependency @types/sinon-chai to v3.2.11

* chore(deps): update dependency sinon to v16.1.1

* chore(deps): update dependency eslint-plugin-import to v2.29.0

* chore(deps): update dependency sinon to v16.1.3

* chore(deps): update dependency eslint to v8.52.0

* chore(deps): update dependency wrangler to v3.15.0

* chore(deps): update dependency @rollup/plugin-replace to v5.0.5

* Add named exports for classes and enums to ESM output
Resolves video-dev#5630

* chore(deps): update dependency @microsoft/api-documenter to v7.23.10

* chore(deps): update dependency @microsoft/api-extractor to v7.38.1

* chore(deps): update dependency @microsoft/api-documenter to v7.23.11

* chore(deps): update dependency @microsoft/api-extractor to v7.38.2

* chore(deps): update typescript-eslint monorepo to v6.9.1

* chore(deps): update typescript-eslint monorepo to v6.10.0

* chore(deps): update dependency eslint to v8.53.0

* Update README.md

* Update README.md

* Update README.md

* chore(deps): update dependency @types/mocha to v10.0.4

* chore(deps): update dependency selenium-webdriver to v4.15.0

* chore(deps): update dependency @types/chart.js to v2.9.40

* chore(deps): update dependency @types/chai to v4.3.10

* chore(deps): update dependency @types/sinon-chai to v3.2.12

* Fix detach attach behavior dropping one of two SourceBuffers

* Use Content Steering Pathways to manage Redundant Streams (video-dev#5970)

* Use Content Steering Pathways to manage Redundant Streams and resolve their errors
* Ensure correct Pathway penalization on Playlist loading errors
* Do not reload Content Steering manifest while media is ended or detached

* chore(deps): update babel monorepo to v7.23.3

* Remove use of `self` from `enableLogger` (video-dev#5936)

Fixes video-dev#5905

* Refactor CMCD controller and tests to use the common media library utilities (video-dev#5903)

* refactor CMCD controller and test to use common media library
* update build script to transpile the @svta package
* add ability to specify cmcd keys

* chore(deps): update dependency lint-staged to v15.1.0

* chore(deps): update dependency @microsoft/api-extractor to v7.38.3

* chore(deps): update typescript-eslint monorepo to v6.11.0

* chore(deps): update typescript-eslint monorepo to v6.12.0

* chore(deps): update dependency @microsoft/api-documenter to v7.23.12

* Fix regression introduced with video-dev#5689 Lazy init CEA608 parsers found in video-dev#5953 (video-dev#5986)

* Fix issues with long cea608 captions. video-dev#5952

In mp4-tools.ts

* Fixed parsing for sei_message, by always consuming the entire message,
  before parsing the message according to payload_type

* Fixed payloadType / payloadSize parsing to ensure they never exceed
  255, as the field is restricted to 8 bytes.

* chore(deps): update dependency node to v20 (video-dev#5928)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update dependency sinon to v17 (video-dev#5944)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update actions/setup-node action to v4 (video-dev#5948)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update dependency chromedriver to v119 [security] (video-dev#5965)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update dependency prettier to v3.1.0 (video-dev#5983)

* chore(deps): update dependency prettier to v3.1.0

* Run prettier

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tom Jenkinson <tom@tjenkinson.me>

* Configure typescript, eslint and prettier caches (video-dev#5990)

* Rollup 4 (video-dev#5886)

* Update karma-rollup-preprocessor to version that works in watch mode (video-dev#5991)

* chore(deps): update dependency @svta/common-media-library to v0.5.1

* chore(deps): update dependency wrangler to v3.16.0

* chore(deps): update dependency wrangler to v3.17.1

* chore(deps): update dependency rollup to v4.5.2

* chore(deps): update dependency eslint to v8.54.0

* API enhancements for audio and subtitle selection
Resolves video-dev#5532

* Remove note about "canplay" that references code removed from the example (related to autoplay policy)
Closes video-dev#3153

* Fix issues parsing sei_messages

In mp4-tools.ts

* Fixed parsing for sei_message to remove the incorrect masking to 8
  bits for the sei message size.

* Remove SEI payload type masking

* chore(deps): update dependency @types/chart.js to v2.9.41

* chore(deps): update dependency @types/chai to v4.3.11

* chore(deps): update dependency @types/mocha to v10.0.5

* chore(deps): update dependency @types/mocha to v10.0.6

* Expand isSupported check to test other codecs
Resolves video-dev#6004

* Add `videoPreference` config option for HDR/SDR VIDEO-RANGE selection and priority
Resolves video-dev#2489

* Recover from media error after MediaSource ended following SourceBuffer update error event

* Fix exception on 2019 Tizen where MediaCapabilities is undefined

* Add `isMSESupported` check
Add named exports for and expose statically: `isSupported`, `isMSESupported`, and `getMediaSource`

* chore(deps): update dependency @rollup/plugin-alias to v5.1.0

* chore(deps): update dependency rollup to v4.6.0

* chore(deps): update dependency rollup to v4.6.1

* chore(deps): update typescript-eslint monorepo to v6.13.0

* chore(deps): update typescript-eslint monorepo to v6.13.2

* chore(deps): update babel monorepo to v7.23.5

* Remove use of deprecated WebKitDataCue and hand Cue instantiation and custom property setting errors
Fixes video-dev#6020

* Add polyfill for isSafeInteger

* Fix esds box parsing for for usac audio

* Update README Compatibility section

* chore(deps): update dependency @svta/common-media-library to v0.6.0

* chore(deps): update dependency wrangler to v3.18.0

* chore(deps): update dependency wrangler to v3.19.0

* chore(deps): update dependency eslint to v8.55.0

* chore(deps): update dependency eslint-config-prettier to v9.1.0

* chore(deps): update dependency lint-staged to v15.2.0

* chore(deps): update dependency @microsoft/api-documenter to v7.23.13

* chore(deps): update dependency @microsoft/api-extractor to v7.38.4

* chore(deps): update dependency @microsoft/api-extractor to v7.38.5

* chore(deps): update dependency @microsoft/api-documenter to v7.23.14

* Use addEventListener for MediaKeySession events
video-dev#6034

* chore(deps): update dependency selenium-webdriver to v4.16.0

* fix(latency-controller): only sync live stream

* chore(deps): update actions/github-script action to v7 (video-dev#5996)

* Ignore #EXT-X-INDEPENDENT-SEGMENTS so that it is not added to Fragment tagList

* Fix handling of the DATERANGE END-ON-NEXT attribute

* chore(deps): update dependency rollup to v4.7.0

* chore(deps): update dependency rollup to v4.9.0

* Store deployments in json file, and generate md and txt file from that (video-dev#6044)

* Fix deployment branch update commit messages

Just noticed this has been broken for a while

* Fix path to deployment readme script

* Fix path to script again

* Add the final `/` at the end of deployment url

* Remove tab at end of deployments readme

Which is causing the list to be really spaced out for some reason

* chore(deps): update dependency typescript to v5.3.3 (video-dev#5999)

* chore(deps): update dependency typescript to v5.3.3

* Include api extractor changes

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tom Jenkinson <tom@tjenkinson.me>

* Exclude PS4 from TextDecoder use

On PS4 TextDecoder is defined but it is partially implemented and does not function properly. This will force manual decoding approach for PS4 platform.

* Ignore #EXT-X-INDEPENDENT-SEGMENTS (video-dev#6047)

Fixes video-dev#6039

* chore(deps): update babel monorepo to v7.23.6

* chore(deps): update dependency prettier to v3.1.1

* chore(deps): update typescript-eslint monorepo to v6.14.0

* chore(deps): update typescript-eslint monorepo to v6.15.0

* chore(deps): update dependency wrangler to v3.20.0

* chore(deps): update dependency wrangler to v3.22.0

* Fix base-stream-controller onHandlerDestroying callback evocation
Remove circular references left after destroying player

* chore(deps): update dependency eslint-plugin-import to v2.29.1

* chore(deps): update dependency eslint to v8.56.0

* chore(deps): update dependency @svta/common-media-library to v0.6.1

* chore(deps): update dependency rollup to v4.9.1

* chore(deps): update dependency @microsoft/api-documenter to v7.23.15

* chore(deps): update dependency @microsoft/api-extractor to v7.39.0

* chore(deps): update dependency chromedriver to v120 (video-dev#6052)

* chore(deps): update github/codeql-action action to v3 (video-dev#6058)

* chore(deps): update dependency wrangler to v3.22.1

* Abort fetch loader as long as loading has not ended
Fixes video-dev#6054

* chore(deps): update dependency chromedriver to v120.0.1

* chore(deps): update typescript-eslint monorepo to v6.16.0

* chore(deps): update typescript-eslint monorepo to v6.17.0

* chore(deps): update babel monorepo to v7.23.7

* chore(deps): update dependency rollup to v4.9.2

* chore(deps): update dependency rollup to v4.9.4

* Fix codec parsing for AVC streams (video-dev#6077)

* Force auto level on emergency switch down (video-dev#6082)

Update estimates on frag load timeout
Do not abort request in _abandonRulesCheck
Remove two segment forward buffer length limit in _abandonRulesCheck
Reset estimate when candidate bitrate is lower than adjusted estimate
Resolves video-dev#6079

* chore(deps): update dependency wrangler to v3.22.2

* chore(deps): update dependency wrangler to v3.22.4

* chore(deps): update dependency @microsoft/api-documenter to v7.23.16

* chore(deps): update dependency @microsoft/api-extractor to v7.39.1

* Null CMCD callbacks on destroy (video-dev#6098)

* Fix regression where subtitle options with AUTOSELECT and FORCED are enabled at start (video-dev#6094)

* Do not enable subtitle options with AUTOSELECT=YES attribute
* Update and add initial selection tests for subtitle-controller
* Only pick forced subtitle option if it is the only one
Add default field to audio and subtitle selection options and forced field to subtitle selection option
* Address TextTrack change event overriding subtitle preference
Fix _TRACKS_UPDATED and _TRACK_SWITCH event order when preference is selected
* Do not auto select subtitle options with FORCED=YES attribute

* Update artifact actions (video-dev#6099)

* Update functional tests to run on Safari using MacOS 13 (video-dev#6101)

* Update functional tests to run on Safari using MacOS 13

* Skip smooth switch test in Safari on streams with overlapping appends

* Omit VOD "ended" event tests with overlapping appends from Safari

* chore(deps): update dependency chai to v4.4.0

* chore(deps): update dependency chai to v4.4.1

* chore(deps): update typescript-eslint monorepo to v6.18.0

* chore(deps): update typescript-eslint monorepo to v6.18.1

* Use AAC SBR (HE-AAC) workaround on Pale Moon (video-dev#6111)

---------

Co-authored-by: hlsjs-ci <40664919+hlsjs-ci@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Rob Walch <rwalch@apple.com>
Co-authored-by: Rob Walch <robwalch@users.noreply.github.com>
Co-authored-by: Casey Occhialini <1508707+littlespex@users.noreply.github.com>
Co-authored-by: Joey Ekstrom <jekstrom@xumo.com>
Co-authored-by: Tom Jenkinson <tom@tjenkinson.me>
Co-authored-by: Tom Jenkinson <tjenkinson@users.noreply.github.com>
Co-authored-by: Evan Burton <eburton2@apple.com>
Co-authored-by: 曾智锋 <zengzhifeng@cvte.com>
Co-authored-by: Agajan Jumakuliyev <agajan.tm@gmail.com>
Co-authored-by: Jakub Perżyło <qizot1405@gmail.com>
Co-authored-by: Pat Nafarrete <pnaf@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment